未处理的承诺拒绝 - 错误:发送后无法设置标头

时间:2017-02-10 00:40:48

标签: node.js express knex.js

我是节点新手,我有一个简单的情况,我在节点/快递应用程序上发布到端点。问题是我得到了:

POST /api/v2/user 500 25.378 ms - 54
(node:19024) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error: Can't set headers after they are sent.
(node:19024) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

我生成的相关代码是:

router.post('/', (req, res, next) => {
  return authHelpers.createUser(req, res)
    .then((user) => {
      return localAuth.encodeToken(user[0]);
    })
    .then((token) => {
      res.status(201).json({
        status: 'success',
        message: 'User Created',
        token: token
      });
    })
    .catch((err) => {
      res.status(500).json({
        status: 'error'
      });
    });
});

然后:

function createUser(req, res) {
  return handleErrors(req)
    .then(() => {
      const salt = bcrypt.genSaltSync();
      const hash = bcrypt.hashSync(req.body.password, salt);
      return knex('users')
        .insert({
          email: req.body.email,
          first_name: req.body.first_name,
          last_name: req.body.last_name,
          username: req.body.username,
          password: hash
        })
        .returning('*');
    })
    .catch((err) => {
      res.status(410).json({
        status: err.message
      });
    });
}

function handleErrors(req) {
  return new Promise((resolve, reject) => {
    if (req.body.username.length < 6) {
      reject({
        message: 'Username must be longer than 6 characters'
      });
    } else if (req.body.password.length < 6) {
      reject({
        message: 'Password must be longer than 6 characters'
      });
    } else {
      resolve();
    }
  });
}

我知道如果我特意删除 res.status(500).json({status:'error'}); ,那么错误就会消失,但我不确定这是正确的。

我的错误究竟是什么以及如何修复的任何线索?

1 个答案:

答案 0 :(得分:5)

您正尝试两次发送回复。首先捕捉错误

  res.status(410).json({
    status: err.message
  });

然后在捕获之后,承诺链继续正常路线,直到:

  return localAuth.encodeToken(user[0]);

哪个失败,因为用户未定义并抛出异常..所以调用错误处理程序并且您尝试再次发送响应,但它失败,因为它已经被发送一次

  res.status(500).json({
    status: 'error'
  });

控制台日志在最后一部分中抛出了哪个错误,我很确定它就像是

  TypeError: Cannot read property '0' of undefined