当尝试使用node.js的方法函数时,undefined不是函数

时间:2015-11-16 19:58:52

标签: javascript node.js

我正在通过node.js处理authenticate函数。 因此,当我尝试使用我在顶部制作的methods.comparePassword函数来验证用户在表单上放置的密码时, 我有一个错误,但无法弄清楚为什么?

首先我有这样的UserSchema。

// user schema
var UserSchema = new Schema({
        name: String,
        username: {
            type: String,
            required: true,
            index: {
                unique: true
            }
        },
        password: {
            type: String,
            required: true,
            select: false
        }
});

var User = mongoose.model('User', UserSchema); 

然后我像这样创建了comparePassword的方法。

// method to compare a given password with the database hash
UserSchema.methods.comparePassword = function(password) {
    var user = this;

    return bcrypt.compareSync(password, user.password);
};

然后我制定了路由进行身份验证,并为这样的用户生成令牌。

apiRouter.post('/authenticate',function(req,res){
    // find the user
    // select the name username and password explicitly
    User.findOne({
        username: req.body.username
    }).select('name username password').exec(function(err, user) {

        if(err) throw err;

        // no user with that username was found
        if(!user){
            res.json({
                success: false,
                message: 'Authentication failed. User not found.'
            });
        } else if(user){

            // check if password matches

            //console.log(req.body.password);
            var validPassword = user.comparePassword(req.body.password);
            //var validPassword = true; If i use this everything works fine.


            if (!validPassword) {
                res.json({
                    success: false,
                    message: 'Authentication failed. Wrong password.'
                });
            } else {

                // if user is found and password is right
                // create a token
                var token = jwt.sign({
                    name: user.name,
                    username: user.username
                }, superSecret, {
                    expiresInMinutes: 1440 // expires in 24 hours
                });

                // return the information including token as JSON
                res.json({
                    success: true,
                    message: 'Enjoy your token!',
                    token: token
                });
            }
        }

    });
});

但是当我向服务器发送请求时,我收到了这样的错误。

var validPassword = user.comparePassword(req.body.password);
                                  ^
TypeError: undefined is not a function
    at Promise.<anonymous> 

然后我改变了var validPassword = true; 一切正常。

任何人都知道如何解决这个问题?

谢谢!

1 个答案:

答案 0 :(得分:1)

确保在创建架构之后但在创建模型之前定义这些方法。

// user schema
var UserSchema = new Schema({
    name: String,
    username: {
        type: String,
        required: true,
        index: {
            unique: true
        }
    },
    password: {
        type: String,
        required: true,
        select: false
    }
});

// method to compare a given password with the database hash
UserSchema.methods.comparePassword = function(password) {
    var user = this;

    return bcrypt.compareSync(password, user.password);
};

var User = mongoose.model('User', UserSchema);