为了账号安全,请及时绑定邮箱和手机立即绑定

在 NodeJS 中引发错误 - UnhandledPromiseRejection

在 NodeJS 中引发错误 - UnhandledPromiseRejection

慕娘9325324 2022-06-09 11:25:55
我对 NodeJS 还很陌生,我仍在试图弄清楚如何处理错误。我读了很多问题,但我不知道我做错了什么。这里我有一个登录功能:export const login = async (req, res) => {let body = req.body;try {  const user = await User.findOne({ username: body.username });if (!user) {  throw new InternalError("Username or password are wrong");}if (!bcrypt.compareSync(body.password, user.password)) {  throw new InternalError("Username or password are wrong");}let token = jwt.sign({ data: user }, "secret", {  expiresIn: 60 * 60 * 24 * 30});return res.json({  user: user,  token: token });} catch (error) {throw new GenericError(error); }};这是我得到的错误,例如,我包含错误的密码:(node:12332) UnhandledPromiseRejectionWarning: GenericError: Username or password are wrong(node:12332) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch()我知道用 Await 做出的承诺必须有一个.then()and.catch()但这不是执行承诺时的错误,而是验证密码时的错误。我想要的是抛出错误并获得响应。现在请求永远不会结束,并且会显示上一个错误。
查看完整描述

1 回答

?
BIG阳

TA贡献1859条经验 获得超6个赞

您收到来自节点的警告,因为您在捕获 InternalError 后重新抛出了 GenericError。与其重新抛出 GenericError,不如在调用时返回响应或捕获 GenericError login。


这是您为两者修改的代码。


export const login = async (req, res) => {

  // We get the body info

  let body = req.body;


  try {

    // We find the user with the username from the body

    const user = await User.findOne({ username: body.username });


    // Let's assume no user exists, so we throw an InternalError,

    // This skips straight to the catch block.

    if (!user) {

      throw new InternalError("Username or password are wrong");

    }


    // We never reach these two statements

    // because of the error above being thrown.

    if (!bcrypt.compareSync(body.password, user.password)) {

      throw new InternalError("Username or password are wrong");

    }


    let token = jwt.sign({ data: user }, "secret", {

      expiresIn: 60 * 60 * 24 * 30

    });


   res.json({

      user: user,

      token: token

    });

  } catch (err) {

    // We caught our InternalError from not having a user above.

    // Now we should return a response that the user is invalid.

    // I am just going to return the error that we previously threw.

   res.json({

      error: err

    });

  }

};

您绝对可以从 catch 块中抛出 GenericError。但是,这样做需要您在调用登录函数的任何地方捕获 GenericError。


export const login = async (req, res) => {

  // We get the body info

  let body = req.body;


  try {

    // We find the user with the username from the body

    const user = await User.findOne({ username: body.username });


    // Let's assume no user exists, so we throw an InternalError,

    // This skips straight to the catch block.

    if (!user) {

      throw new InternalError("Username or password are wrong");

    }


    // We never reach these two statements

    // because of the error above being thrown.

    if (!bcrypt.compareSync(body.password, user.password)) {

      throw new InternalError("Username or password are wrong");

    }


    let token = jwt.sign({ data: user }, "secret", {

      expiresIn: 60 * 60 * 24 * 30

    });


   res.json({

      user: user,

      token: token

    });

  } catch (err) {

    // Throw the generic error

    throw GenericError(err);

  }

};



try {

  // We call the login function that is defined above.

  // It is going to throw a GenericError, so we have to

  // catch it.

  await login(req, res);

} catch (err) {

  // Now we need to catch the generic error

  res.json({

    error: err

  });

}


查看完整回答
反对 回复 2022-06-09
  • 1 回答
  • 0 关注
  • 2646 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信