gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 22:06:06 32 4
gpt4 key购买 nike

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

我在调用passportjs setPassword()时收到this.set is not a function

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 开始,passport-local-mongoose 通过为所有实例和静态方法返回 Promises 来启用异步/等待(serializeUser 和 deserializeUser 除外)。

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/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com