gpt4 book ai didi

javascript - 范围错误 [ERR_HTTP_INVALID_STATUS_CODE] : Invalid status code: [object Object] error express 4. 16

转载 作者:行者123 更新时间:2023-12-01 02:29:11 27 4
gpt4 key购买 nike

我正在实现密码重置,当单击生成的链接时,我收到以下错误,

Thu Jan 25 2018 16:54:38 GMT+0000 (GMT): GET /users/reset/d590c54753abc449cfcd8aefceb8d519fce4b627
express deprecated res.redirect(url, status): Use res.redirect(status, url) instead routes/users.js:297:9
events.js:136
throw er; // Unhandled 'error' event
^

RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: [object Object]
at ServerResponse.writeHead (_http_server.js:193:11)
at ServerResponse.writeHead (/Users/benbagley/Code/poetry-out-loud/node_modules/on-headers/index.js:55:19)
at ServerResponse._implicitHeader (_http_server.js:184:8)
at write_ (_http_outgoing.js:644:9)
at ServerResponse.write (_http_outgoing.js:629:10)
at writetop (/Users/benbagley/Code/poetry-out-loud/node_modules/express-session/index.js:290:26)
at ServerResponse.end (/Users/benbagley/Code/poetry-out-loud/node_modules/express-session/index.js:338:16)
at ServerResponse.redirect (/Users/benbagley/Code/poetry-out-loud/node_modules/express/lib/response.js:947:10)
at /Users/benbagley/Code/poetry-out-loud/routes/users.js:297:9
at model.Query.<anonymous> (/Users/benbagley/Code/poetry-out-loud/node_modules/mongoose/lib/model.js:4056:16)
at /Users/benbagley/Code/poetry-out-loud/node_modules/kareem/index.js:273:21
at /Users/benbagley/Code/poetry-out-loud/node_modules/kareem/index.js:131:16
at process._tickCallback (internal/process/next_tick.js:150:11)
[nodemon] app crashed - waiting for file changes before starting...

这是我的路线

// Token URL :get
router.get('/users/reset/:token', (req, res) => {
User.findOne({
resetPasswordToken: req.params.token,
resetPasswordExpires: { $gt: Date.now() }
}, function(err, user) {
if (!user) {
req.flash('error', 'Password reset token is invalid or has expired.');
res.redirect('/users/forgot');
}

res.redirect('reset.hbs', {
pageTitle: 'Reset Your Password',
user: req.user
});
});
});

// Token URL :post
router.post('/users/reset/:token', (req, res, next) => {
if(req.body.password === req.body['password-confirm']) {
next();
return;
}

req.flash('error', 'Passwords do not match!');
res.redirect('back');

User.findOne({
resetPasswordToken: req.params.token,
resetPasswordExpires: { $gt: Date.now() }
}, function(err, user) {
if(!user) {
req.flash('error', ' Password reset is invalid or has expired');
res.redirect('/login');
}

const setPassword = promisify(user.setPassword, user);
setPassword(req.body.password);
user.resetPasswordToken = undefined;
user.resetPasswordExpires = undefined;
const updatedUser = user.save();
req.login(updatedUser);
req.flash('success_msg', 'Your password has been reset successfully! You are now logged in!');
res.redirect('/dashboard');
});
});

我想,我已经接近让它工作了,只是遇到了一些错误。这就是其中之一。我检查了文档,没有看到任何提到状态代码的内容,我相信这是最近框架的一个变化,必须为每个重定向或渲染定义状态代码似乎很奇怪。

最佳答案

您必须使用res.render而不是res.redirect :

res.render('reset.hbs', {
pageTitle: 'Reset Your Password',
user: req.user
});

res.redirect方法接受路由字符串作为参数,而 res.render接受表示要渲染的 View 的文件路径的字符串

关于javascript - 范围错误 [ERR_HTTP_INVALID_STATUS_CODE] : Invalid status code: [object Object] error express 4. 16,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48448321/

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