【问题标题】:Mongoose schema method is "not a function"猫鼬模式方法“不是函数”
【发布时间】:2015-12-15 16:32:36
【问题描述】:

我的模型看起来像这样,但是当我尝试使用 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."
})

【问题讨论】:

    标签: node.js mongodb mongoose koa


    【解决方案1】:

    Mongo 的“查找”返回结果的可迭代游标(可能没有)。如果您希望只得到一个结果,请尝试“findOne”。这将返回一个文档。

    【讨论】:

    • 换句话说,模式方法仅在有单个结果时可用。
    猜你喜欢
    • 2021-04-09
    • 2016-04-22
    • 2015-09-20
    • 2023-01-28
    • 2020-07-27
    • 2018-01-15
    • 2019-01-18
    • 2021-07-14
    • 1970-01-01
    相关资源
    最近更新 更多