1
我的模型看起來是這樣的,但是當我嘗試使用verifyPassword
,它說:TypeError: user.verifyPassword is not a function
貓鼬架構的方法是「不是一個函數」
const User = new mongoose.Schema({
name: {
type: String,
required: true
},
password: {
type: String,
required: true
},
avatar: String,
token: String,
role: String,
permissions: Array,
email: {
type: String,
unique: true,
required: true
},
joined: {
type: Number,
default: (new Date() * 1)
}
})
User.methods.verifyPassword = function (password) {
return bcrypt.compare(password, this.password)
}
我這樣使用它。
yield User.find({
email: this.request.body.email
}).exec()
.then(user => {
if (user.verifyPassword(self.request.body.password)) {
self.status = 200
self.body = {
token: user.token
}
} else {
self.status = 500
self.body = "Problem signing in."
}
}, error => {
self.status = 500
self.body = "Problem signing in."
})