我在使用passportJS 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)
最佳答案
万一有人遇到同样的问题,
从版本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');
关于node.js - Passportjs调用setPassword时this.set不是函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54066687/