UnhandledPromiseRejectionWarning: This error originated either by throwing inside of an async function without a catch block

后端 未结 4 2226
猫巷女王i
猫巷女王i 2020-12-08 19:16

I am getting following error in my Node-Express App

UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by

4条回答
  •  暖寄归人
    2020-12-08 19:37

    I suggest removing the below code from getMails

     .catch(error => { throw error})
    

    In your main function you should put await and related code in Try block and also add one catch block where you failure code.


    you function gmaiLHelper.getEmails should return a promise which has reject and resolve in it.

    Now while calling and using await put that in try catch block(remove the .catch) as below.

    router.get("/emailfetch", authCheck, async (req, res) => {
      //listing messages in users mailbox 
    try{
      let emailFetch = await gmaiLHelper.getEmails(req.user._doc.profile_id , '/messages', req.user.accessToken)
    }
    catch (error) { 
     // your catch block code goes here
    })
    

提交回复
热议问题