Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.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 ExpressJS应用程序登录在保存第一次登录历史记录后失败_Node.js_Mongodb_Express_Mongoose_Passport.js - Fatal编程技术网

Node.js ExpressJS应用程序登录在保存第一次登录历史记录后失败

Node.js ExpressJS应用程序登录在保存第一次登录历史记录后失败,node.js,mongodb,express,mongoose,passport.js,Node.js,Mongodb,Express,Mongoose,Passport.js,我已经使用PassportJS、MongoDB(Mongoose)和JSON web令牌实现了一个ExpressJS应用程序 内部成功登录路由实现 await user.insertLoginTime(); const token = user.generateJwt(); res.status(200).json({ token }); Mongoose模型方法“insertLoginTime” userSchema.methods.insertLoginTime = async funct

我已经使用PassportJS、MongoDB(Mongoose)和JSON web令牌实现了一个ExpressJS应用程序

内部成功登录路由实现

await user.insertLoginTime();
const token = user.generateJwt();
res.status(200).json({ token });
Mongoose模型方法“insertLoginTime”

userSchema.methods.insertLoginTime = async function() {
  try {
    const presentTime = new Date().getTime();
    await this.loginHistory.push(presentTime);
    await this.save();
    return;
  } catch (error) {
      throw new Error(error);
  }
};
第一次登录成功,“邮递员”收到令牌,但在下次尝试时返回无效密码错误


删除“insertLoginTime”方法成功登录多次尝试。“insertLoginTime”实现中是否存在错误?

我已解决此问题。这是因为我为“save”的模式“pre”钩子添加了密码哈希。在保存登录历史记录时,将再次对哈希密码进行哈希并将其保存回。这是在删除实际的密码哈希,并导致下一次尝试失败。

我已解决此问题。这是因为我为“save”的模式“pre”钩子添加了密码哈希。在保存登录历史记录时,将再次对哈希密码进行哈希并将其保存回。这是在删除实际的密码哈希并导致下一次尝试失败