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

时间:2018-01-25 17:03:40

标签: javascript node.js express express-4

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

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');
  });
});

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

1 个答案:

答案 0 :(得分:2)

您必须使用here代替res.render

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

res.redirect方法接受路由字符串作为争论,而res.redirect接受表示要呈现的视图的文件路径的字符串