猫鼬模式方法"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"返回一个可迭代的结果游标(可能没有结果)。如果您希望只得到一个结果,请尝试"findOne"。这将返回一个文档。

相关内容

最新更新