'Mongoose ValidationError makes Express Server crashed
I have struggled with this problem for many days.
Whenever I make an invalid POST request, it throws me an Error. Sounds good but when I cancel that request, the server closes to???
This is an invalid request.
{
"title": "Test from Postman",
"description": "Test from Postman",
"time": "2021-05-12",
"status": "abc"
}
This is the error I get.
You guys can look through my repo here. Maybe the error comes from mongoose validation
Thank you.
Solution 1:[1]
I had this problem too. The issue was that I was catching the error in two places: one in the server file and one in the app file. Removing the catch error from the server file allowed the error to be caught in the app file, and handled in the app file correctly.
Wrong code:
// server file
const create = (person) => {
const request = axios.post(baseUrl, person)
return request
.then((response) => response.data)
.catch((error) => console.log(error)) // this is the problem
}
Right code:
// server file
const create = (person) => {
const request = axios.post(baseUrl, person)
return request
.then((response) => response.data)
// the catch line has been removed here because the error is caught elsewhere
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Green |