Mongoose 模式方法是 "not a function"
Mongoose schema method is "not a function"
我的模型看起来像这样,但是当我尝试使用 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."
})
Mongo 的 "find" return 是结果的可迭代游标(可能有 none)。如果您希望只获得一个结果,请尝试 "findOne"。这将 return 一份文件。
我的模型看起来像这样,但是当我尝试使用 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."
})
Mongo 的 "find" return 是结果的可迭代游标(可能有 none)。如果您希望只获得一个结果,请尝试 "findOne"。这将 return 一份文件。