javascript 参考错误:用户未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23289925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ReferenceError: User is not defined
提问by yeswecode
I've got an error while i try to auth via vkontakte (vk.com) (passport-vkontakte)
Error:ReferenceError: User is not defined
Here is my auth.js
当我尝试通过 vkontakte (vk.com) (passport-vkontakte) 进行身份验证时出现
错误:ReferenceError:用户未定义
这是我的auth.js
var express = require('express');
var passport = require('passport'), VKontakteStrategy = require('passport-vkontakte').Strategy;
var router = express.Router();
var app = express();
router.get('/', function(req, res) {
res.send('Auth');
});
passport.use(new VKontakteStrategy({
clientID: 000, // VK.com docs call it 'API ID'
clientSecret: '***',
callbackURL: "http://127.0.0.1:3000/auth/vkontakte/callback"
},
function(accessToken, refreshToken, profile, done) {
User.findOrCreate({ vkontakteId: profile.id }, function (err, user) {
return done(err, user);
});
}
));
router.get('/vkontakte',
passport.authenticate('vkontakte'),
function(req, res){
// The request will be redirected to vk.com for authentication, so
// this function will not be called.
});
router.get('/vkontakte/callback',
passport.authenticate('vkontakte', { failureRedirect: '/' }),
function(req, res) {
// Successful authentication, redirect home.
var User = req.User;
res.redirect('/');
});
router.get('/logout', function(req, res){
req.logout();
res.redirect('/');
});
module.exports = router;
Express ver: 4
快递版本:4
回答by Rafael
Define User Model
定义用户模型
Mongoose or Mongoskin Example
猫鼬或猫鼬皮示例
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/my_database');
var Schema = mongoose.Schema
, ObjectId = Schema.ObjectId;
var BlogPost = new Schema({
author : ObjectId
, title : String
, body : String
, date : Date
});
var Post = mongoose.model('ModelName', BlogPost);
Post.findOrCreate({ id: QUERY }, function (err, docs) {
});
You need to create a model so your strategy can interact with your application properly. In your case, this model will be for users so it is appropriately named User
as an entity.
您需要创建一个模型,以便您的策略可以与您的应用程序正确交互。在您的情况下,此模型将用于用户,因此它被适当地命名User
为实体。
Boilerplate Code to Get You Started
样板代码助您入门
This code was added from the strategy developers to get you started.
此代码是由策略开发人员添加的,以帮助您入门。
User.findOrCreate({ vkontakteId: profile.id }, function (err, user) {
return done(err, user);
});
It's the same concept, hope this clears things up.
这是相同的概念,希望这可以解决问题。