Passportjs调用setPassword时this.set不是函数

时间:2019-01-06 22:59:18

标签: node.js passport.js

我遇到了护照JS setPassword的问题。该代码在以前的项目中工作(passport-local-mongoose:4.0.0,护照:0.3.2),但是,现在使用较新版本时,我遇到了问题(passport-local-mongoose:5.0.1,护照:0.4。 0。

我收到this.set是不是在调用通行证js setPassword()时的功能

const {promisify} = require('es6-promisify');

exports.resetPassword = async (req, res) => {
  const user = await User.findOne({
    resetPasswordToken: req.params.token,
    resetPasswordExpires: { $gt: Date.now() }
  });
  
  if (!user) {
    req.flash('error', 'Password reset is invalid or has expired');
    return res.redirect('/login');
  }

  const setPassword = promisify(user.setPassword, user);
  await setPassword(req.body.password);

  user.resetPasswordToken = undefined;
  user.resetPasswordExpires = undefined;
  const updatedUser = await user.save();
  await req.login(updatedUser);
  req.flash('success', 'Your password has been reset!');
  res.redirect('/login');
}

错误。

TypeError: this.set is not a function
    at Promise.resolve.then.then.then.then.then.salt (...\node_modules\passport-local-mongoose\index.js:98:14)
From previous event:
    at exports.updatePassword (...auth.controller.js:146:9)
    at 
    at process._tickCallback (internal/process/next_tick.js:188:7)

1 个答案:

答案 0 :(得分:1)

以防万一其他人遇到相同的问题,

从5.0.0版开始,通过为所有实例和静态方法(序列化用户和deserializeUser除外)返回Promises,从而启用了护照/本地猫鼬异步/等待功能。

const user = new DefaultUser({username: 'user'});
await user.setPassword('password');
await user.save();
const { user } = await DefaultUser.authenticate()('user', 'password');