如何修复我的代码?警告:已创建承诺

时间:2019-03-16 11:01:27

标签: javascript

一段时间以来,我在控制台中犯了一个错误,而且我不太了解正在发生的事情以及应该如何解决。错误:

有人可以告诉我如何写得更好吗?

Warning: a promise was created in a handler at /Users/pietrzakadrian/Desktop/bankApplicationOfficial/server/controllers/user.controller.js:119:16 but was not returned from it, see
    at Function.Promise.attempt.Promise.try (/Users/pietrzakadrian/Desktop/bankApplicationOfficial/node_modules/bluebird/js/release/method.js:29:9)

这是我的代码,我在应该有错误的行中添加了注释?

// Login Action
exports.login = (req, res) => {
  function getTodayDate() {
    const today = new Date();
    return today;
  }

  function getToken(userId) {
    const token = jwt.sign(
      {
        id: userId,
      },
      env.SECRET_KEY,
      {
        expiresIn: '60min',
      },
    );
    return token;
  }

  User.findOne({
    where: {
      login: req.body.login,
    },
  })
    .then(isUser => {
      if (isUser) {
        if (bcrypt.compareSync(req.body.password, isUser.password)) {
          User.update( // <- this
            {
              last_present_logged: getTodayDate(),
            },
            { where: { login: req.body.login } },
          ).then(() => {
            res.status(200).json({
              success: true,
              token: getToken(isUser.id),
            });

          });
        } else {
          User.update(
            {
              last_failed_logged: getTodayDate(),
            },
            { where: { login: req.body.login } },
          ).then(() => {
            res.status(200).json({
              error: 'Auth failed. The password is incorrect.',
              success: false,
            });
          });
        }
      } else {
        res
          .status(200)
          .json({ error: 'Auth failed. User does not exist', success: false });
      }
    })
    .catch(() => {
      /* just ignore */
    });
};

1 个答案:

答案 0 :(得分:0)

Bluebird的文档(请查看位于Warning explanations section上的示例:

  

这通常意味着您只是忘记了某个地方的return语句,这将导致未与任何承诺链连接的失控承诺。