Hello Guys, How are you all? Hope You all Are Fine. Today I am getting the following error in my Node-Express App UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block in nodejs. So Here I am Explain to you all the possible solutions here.
Without wasting your time, Let’s start This Article to Solve This Error.
- How UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block Error Occurs ?
- How To Solve UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block Error ?
- Solution 1
- Solution 2
- Summery
How UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block Error Occurs ?
I am Using express in my node js application But I am getting following error in my Node-Express App.
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(). (rejection id: 4)
Here is my code Where I am facing an error.
router.get("/checkEmail", authCheck, async (req, res) => {
.catch(error => { throw error})
emailFetch = emailFetch.data
res.send(emailFetch)
})
How To Solve UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block Error ?
- How To Solve UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block Error?
To Solve UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block Error Here is in your code you are using .catch(error => { throw error}) which results in unhandled rejection in route handler. You can use it as like below.
- UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block
To Solve UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block Error Here is in your code you are using .catch(error => { throw error}) which results in unhandled rejection in the route handler. You can use it as like below.
Solution 1
Here is in your code you are using .catch(error => { throw error}) which results in unhandled rejection in route handler. You can use it as like below.
router.get("/yourApicall", authCheck, async (req, res, next) => {
try {
data= yourdata
res.send(data)
} catch (err) {
next(err);
}
})
Solution 2
I suggest removing the below code from your code .catch(error => { throw error}). In your main function, you should put await and related code in a Try block and also add one catch block where you failure code. You can use try-catch as define below.
router.get("/apicall", authCheck, async (req, res) => {
try{
// your code
}
catch (error) {
// your catch block code goes here
})
Summery
It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you?
Also Read