【问题标题】:can't understand ".this" in mongoose virtual无法理解猫鼬虚拟中的“.this”
【发布时间】:2021-01-03 14:49:15
【问题描述】:

userSchema.virtual("password")
      .set(function(password) {
          //temp var
          this._password = password;

          //gen salt
          this.salt = this.makeSalt();

          //encrypt pass
          this.hashed_password = this.encryptPassword(password);

      })
      .get(function () {
          return this._password;
      });


     
   



我想知道上面代码中“this”所指的范围。我有一个名为 userSchema 的 mongo 模式,我正在为其设置一个虚拟字段,即密码。只是好奇 this 在这里指的是什么通常在函数调用中 this 指的是全局范围。

【问题讨论】:

    标签: javascript node.js reactjs mongodb mongoose


    【解决方案1】:

    这是指目标字段。

    附带说明,如果您尝试加密密码,我会使用“pre”而不是虚拟来存储加密密码

    【讨论】:

    • 谢谢伙计。我没有将密码存储为虚拟密码,而是用户输入的密码。
    猜你喜欢
    • 2016-06-18
    • 1970-01-01
    • 2020-08-29
    • 2021-01-09
    • 2020-05-18
    • 1970-01-01
    • 2017-04-04
    • 1970-01-01
    • 2021-09-27
    相关资源
    最近更新 更多