且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Node.js和Passport对象没有方法validPassword

更新时间:2023-12-04 11:14:04

您正在使用

if (!user.validPassword(password)) {
    return done(null, false, { message: 'Incorrect password.' });
}

但是您尚未定义 validPassword 方法.将其附加到您的架构:

but you haven't defined validPassword method. Attach it to your schema:

var authSchema = mongoose.Schema({ 
    username: 'string',
    password: 'string'
});
authSchema.methods.validPassword = function( pwd ) {
    // EXAMPLE CODE!
    return ( this.password === pwd );
};

编辑,您还错误地定义了架构.应该是:

EDIT You've also incorrectly defined the schema. It should be:

var authSchema = mongoose.Schema({ 
    username: String,
    password: String
});

请注意,用户名密码均应为 String 类型的对象,而不是字符串"string" ,如果你明白我的意思.:)

Note that both username and password should be String type objects, not strings "string", if you know what I mean. :)