Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 在Mongoose方法上使用bcrypt时出现未经处理的PromisejectionWarning_Node.js_Express_Mongoose_Bcrypt - Fatal编程技术网

Node.js 在Mongoose方法上使用bcrypt时出现未经处理的PromisejectionWarning

Node.js 在Mongoose方法上使用bcrypt时出现未经处理的PromisejectionWarning,node.js,express,mongoose,bcrypt,Node.js,Express,Mongoose,Bcrypt,正在尝试实现重置密码功能并不断获取未处理的PromisejectionWarning:错误:非法参数:未定义,执行此操作时出现字符串错误。我在其他项目中使用过这段代码,它工作得非常完美,但由于某些原因,我似乎无法确切地理解为什么会出现这种情况,或者下面的代码可能有什么问题 方法: UserSchema.methods.hashPassword = () => { return new Promise((resolve, reject) => { bcrypt.genSal

正在尝试实现重置密码功能并不断获取未处理的PromisejectionWarning:错误:非法参数:未定义,执行此操作时出现字符串错误。我在其他项目中使用过这段代码,它工作得非常完美,但由于某些原因,我似乎无法确切地理解为什么会出现这种情况,或者下面的代码可能有什么问题

方法:

UserSchema.methods.hashPassword = () => {
  return new Promise((resolve, reject) => {
    bcrypt.genSalt(10, (err1, salt) => {
      if (err1) {
        reject(err1);
      }
      bcrypt.hash(this.password, salt, (err2, hash) => { // <-- it appears to happen on this line at ".hash"
        if (err2) {
          reject(err2);
        }
        this.password = hash;
        resolve(hash);
      });
    });
  });
};
const resetPassword = async (req, res) => {

      // Update User
      const { password } = req.body;
      user.password = password;
      user.passwordResetToken = '';
      user.passwordResetExpires = moment().utcOffset(0);

      user.hashPassword().then(() => { <---- Happens here
        // Save Updated User to the Database
        user.save((error) => {
          if (error) {
            return res.status(500).json({
              success: false,
              message: 'An unexpected error occurred.',
            });
          }

          // Send Email Confirming Password Change to the User
          
        });
      });
    });
  });
};
UserSchema.methods.hashPassword=()=>{
返回新承诺((解决、拒绝)=>{
bcrypt.genSalt(10,(错误1,盐)=>{
如果(错误1){
拒绝(err1);
}
bcrypt.hash(this.password,salt,(err2,hash)=>{//{
//更新用户
const{password}=req.body;
user.password=密码;
user.passwordResetToken='';
user.passwordResetExpires=moment().utcOffset(0);
user.hashPassword()。然后(()=>{{
如果(错误){
返回res.status(500).json({
成功:错,
消息:“发生意外错误。”,
});
}
//向用户发送确认密码更改的电子邮件
});
});
});
});
};

bcrypt对我来说也不管用,但bcryptjs管用 删除bcrypt后尝试导入“bcryptjs”

你可以从这里把它引下去


然后将其导入您的文件并重试

然后可能会回调。返回“完成”将解决此问题?我建议您尝试使用代码示例而不是链接来澄清您的答案