ReferenceError:未定义用户

时间:2017-09-06 13:04:34

标签: javascript node.js jwt passport.js

这是一个非常具体的问题,但我真的不知道导致错误的原因。我有以下代码:

router.get('/user/profile', passport.authenticate('jwt', { session: false }), function(req, res, next){
// Something
});

当我访问路线时,它会通过我的passport-jwt代码,其中包含我在用户模型中定义的自定义getUserById()函数

let opts = {};
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderWithScheme('jwt');
opts.secretOrKey = config.secret;
passport.use(new JwtStrategy(opts, function(jwt_payload, done){
    Users.getUserById(jwt_payload._id, function(err, user){
        if(err){
            return done(err, false);
        }

        if(user){
            return done(null, user);
        } else {
            return done(null, false);
        }
    });
}));

getUserById()

module.exports.getUserById = function(id, callback){
    Users.findById(id, callback);
}

当我访问路由时,我的服务器会吐出以下错误:

ReferenceError: Users is not defined
    at Function.module.exports.getUserById (E:\Projects\CSGOOrb\models\users.js:55:2)
    at JwtStrategy._verify (E:\Projects\CSGOOrb\config\passport.js:18:9)
    at E:\Projects\CSGOOrb\node_modules\passport-jwt\lib\strategy.js:123:34
    at E:\Projects\CSGOOrb\node_modules\jsonwebtoken\verify.js:27:18
    at _combinedTickCallback (internal/process/next_tick.js:131:7)
    at process._tickCallback (internal/process/next_tick.js:180:9)

我在所有使用它的文件中都需要我的用户模型。有谁知道还有什么可能导致它?

编辑:

passport.js

const colors = require('colors/safe');
const Users = require('../models/users');
const config = require('../config/database');

const jwt = require('jsonwebtoken');
const JwtStrategy = require('passport-jwt').Strategy;
const ExtractJwt = require('passport-jwt').ExtractJwt;
const SteamStrategy = require('passport-steam').Strategy;

module.exports = function(passport){

    let opts = {};
    opts.jwtFromRequest = ExtractJwt.fromAuthHeaderWithScheme('jwt');
    opts.secretOrKey = config.secret;
    passport.use(new JwtStrategy(opts, function(jwt_payload, done){
        Users.getUserById(jwt_payload._id, function(err, user){
            if(err){
                return done(err, false);
            }

            if(user){
                return done(null, user);
            } else {
                return done(null, false);
            }
        });
    }));
...

users.js(猫鼬模型)

const mongoose = require('mongoose');
const config = require('../config/database');

// Users Schema
const UsersSchema = mongoose.Schema(
    {
        admin: {
            type: Boolean,
            default: false
        },
        mod: {
            type: Boolean,
            default: false
        },
        user: {
            type: Boolean,
            default: true
        },
        steam: {
            id: {
                type: String
            },
            identifier: {
                type: String
            },
            name: {
                type: String
            },
            avatar: {
                type: String
            },
            avatarmedium: {
                type: String
            },
            avatarfull: {
                type: String
            },
            tradelink: {
                type: String
            }
        }
    },
    {
        timestamps: 
        { 
            createdAt: 'created_at',
            updatedAt: 'updated_at' 
        }
    }
    );

const Users = module.exports = mongoose.model('User', UsersSchema);

module.exports.getUserById = function(id, callback){
    Users.findById(id, callback);
}

module.exports.getUserById64 = function(id, callback){
    const query = {id: steam.id};
    Users.findOne(query, callback);
}

1 个答案:

答案 0 :(得分:0)

经过一些更多测试后自己想出来了。我添加了

const Users = 

前面的

module.exports = mongoose.model('User', UsersSchema);

所以现在它

const Users = module.exports = mongoose.model('User', UsersSchema);

现在效果很好。还要非常感谢@Yury Tarabanko在评论中提出的建议。