ReferenceError:未定义用户 [英] ReferenceError: User is not defined

查看:129
本文介绍了ReferenceError:未定义用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试通过vkontakte(vk.com)(passport-vkontakte)进行身份验证时遇到错误

错误: 参考错误:未定义用户

这是我的 auth.js

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

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;

速成版:4

推荐答案

定义用户模型

猫鼬或猫鼬皮示例

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) {

});

您需要创建一个模型,以便您的策略可以与您的应用程序正确交互.在您的情况下,此模型适用于用户,因此应适当地将其命名为User作为实体.

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.

此代码是从策略开发人员添加的,以帮助您入门.

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.

这篇关于ReferenceError:未定义用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆