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

Tho*_*ski 2 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.
Run Code Online (Sandbox Code Playgroud)

我生成的相关代码是:

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'
      });
    });
});
Run Code Online (Sandbox Code Playgroud)

然后:

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();
    }
  });
}
Run Code Online (Sandbox Code Playgroud)

我知道如果我删除res.status(500).json({status:'error'}); 特别是,然后错误就消失了,但我不确定这是否合适.

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

Mik*_*stö 5

您正尝试两次发送响应.首先捕获错误

  res.status(410).json({
    status: err.message
  });
Run Code Online (Sandbox Code Playgroud)

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

  return localAuth.encodeToken(user[0]);
Run Code Online (Sandbox Code Playgroud)

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

  res.status(500).json({
    status: 'error'
  });
Run Code Online (Sandbox Code Playgroud)

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

  TypeError: Cannot read property '0' of undefined
Run Code Online (Sandbox Code Playgroud)