我使用 Mongoose 定义了一个实例方法来验证代表(用户):
RepSchema.methods.authenticate = function(password){
return this.encryptPassword(password) === this.hashed_password;
};
在我的应用程序中,我找到代表并致电authenticate
其方法:
var mongoose = require("mongoose");
var Rep = mongoose.model("Rep");
Rep.findOne({email: email}, function(err, rep){
if (rep.authenticate(req.body.session.password)){
req.session.rep_id = rep._id;
res.redirect('/calls', {});
}
});
但是我收到这个错误:
TypeError: Object { email: '[email protected]',
password: XXXXXXXXX,
name: 'meltz',
_id: 4fbc6fcb2777fa0272000003,
created_at: Wed, 23 May 2012 05:04:11 GMT,
confirmed: false,
company_head: false } has no method 'authenticate'
我究竟做错了什么?
所以我终于明白我做错了什么。 mongoose源码应用了里面所有定义的方法schema.methods
模型的架构设置为模型名称时的模型原型(mongoose.model("modelname", modelSchema)
)。因此,在将模型设置为其名称之前,您必须定义所有方法,这会将这些方法添加到 Schema 实例的方法对象中。我在定义方法之前设置模型。问题解决了。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)