RangeError [ERR_HTTP_INVALID_STATUS_CODE]: 无效的状态码: [object Object] error express 4.16

RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: [object Object] error express 4.16

我正在实施密码重置,当点击生成的 link 我收到以下错误,

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 instead of res.redirect:

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

res.redirect method accept the route string as arguement while res.render 接受表示要呈现的视图的文件路径的字符串