Javascript 在另一个函数中使用此实例访问用户密码变量

Javascript 在另一个函数中使用此实例访问用户密码变量,javascript,bcrypt,mongoose-schema,Javascript,Bcrypt,Mongoose Schema,我正在尝试验证用户身份,但我在postman中遇到此错误 { "error": "data and hash arguments required" } 我的用户模型如下所示: const mongoose = require('mongoose') const bcrypt = require('bcrypt') const SALT_WORK_FACTOR = 10 const Schema = mongoose.Schema const UserSchema = new Schema(

我正在尝试验证用户身份,但我在postman中遇到此错误

{
"error": "data and hash arguments required"
}
我的用户模型如下所示:

const mongoose = require('mongoose')
const bcrypt = require('bcrypt')
const SALT_WORK_FACTOR = 10
const Schema = mongoose.Schema

const UserSchema = new Schema({
  username: { type: String, required: true, index: { unique: true } },
  password: { type: String, required: true }
})
UserSchema.pre('save', function (next) {
  let user = this

  // only hash the password if it has been modified(or is new)
  if (!user.isModified('password')) return next()

  // generate a salt
  bcrypt.genSalt(SALT_WORK_FACTOR, (err, salt) => {
    if (err) return next()

    // hash the password along with our new salt
    bcrypt.hash(user.password, salt, (err, hash) => {
      if (err) return next(err)
      // override the cleartext password with the hashed one
      user.password = hash
      next()
    })
  })
})
UserSchema.methods.comparePassword = (candidatePassword, callback) => {
  console.log('Password', this.password)// undefined
  bcrypt.compare(candidatePassword, this.password, (err, isMatch) => {
    callback(err, isMatch)
  })
}
module.exports = mongoose.model('userData', UserSchema, 'userData')

我发现
this。传递给bcrypt compare函数的密码
解析为未定义。我如何访问上面UserSchema实例中定义的密码变量?

显然,在javascript中,
this
在对象调用函数之前不会被赋值,从而将
绑定到方法的所有者(调用对象)。 在上面的问题中,我使用的是
es6
箭头函数,其
this
绑定到紧邻的周边范围(词法范围),因此找不到属性
password
,该属性在用户模式上定义,并且在箭头函数中未绑定到
this
的范围。
例如:

注意
这个
在es6 arrow函数中指向person对象,这是它的静态范围(词法)。让我们从es5的角度来看同一段代码

 var Utils = {
      printName: function(somename) {
        console.log(somename)
      }
    }

    var person = {
      firstName: "Gregory",
      lastNames: ["Baltimore", "Barry", "Derrick", "Evanson"],
      fullName: function() {
        this.lastNames.forEach(function(lastname) {
    //this points to global object
          Utils.printName(this.firstName + " " + lastname)  //output undefined Baltimore undefined Barry undefined Derrick undefined Evanson 
        })
      }
    }

    person.fullName()
您会注意到firstname打印为未定义。这是因为在es5
中,此
超出范围,因此默认情况下(Javascript怪癖)绑定到全局对象。在浏览器中,此全局对象是Nodejs中的窗口,如果用户未定义,则全局对象是Nodejs环境(运行时)。(例如,在模块导出中) 为了解决我的问题,我默认使用es5函数,因为password属性 绑定到我定义为UserSchema的全局对象。因此密码属性已正确解析

 UserSchema.methods.comparePassword = function (candidatePassword, callback) {
  //Now this is bound to the UserSchema object
  bcrypt.compare(candidatePassword, this.password, (err, isMatch) => {
    callback(err, isMatch)
  })
}
更多的信息可以在这里找到

这里也是

 UserSchema.methods.comparePassword = function (candidatePassword, callback) {
  //Now this is bound to the UserSchema object
  bcrypt.compare(candidatePassword, this.password, (err, isMatch) => {
    callback(err, isMatch)
  })
}