Issue with throwing error
there is something confusing here in express
how i got 200 response in the both cases if i find a book or not
this is the error handler middleware
and this is the function
34 Replies
I'm not super familiar with express or its middleware, but
Are you sure you want to use
res.statusCode
? https://expressjs.com/en/4x/api.html#res This seems to suggest res doesn't have a statusCode property
that would mean the statusCode
variable would be 500, which is a case you're not handling
(also, you can write that as const statusCode = res.statusCode ?? 500;
instead of having res.statusCode
twice)
hm, wait, it does log out the 200
doesn't it. Maybe Books.findById
is returning something truthy even if it doesn't find anything? What ORM are you using?yep that what i was going to tell you that it logs out 200 when it goes to the error handler
im using mongoose
seems like you need to call
exec()
on findById https://mongoosejs.com/docs/api/model.html#Model.findById()
which would return null, which is falsey, so it should work as expectedsame issue i tried to log the result from the findById it doesn't log any thing if not found
can you change it to
console.log(typeof book);
?if found => object
not found doesn't log anything
that is very strange, even if it is returning null it should still log out something, in both situations btw
with the ID that should 404, is it still matching with this route?
but it goes inside the if because if it didn't then it will not log the res.statusCode
that's in the middleware though
idk how it goes inside the if and still response with 200
it looks like the 404 isn't hitting
updateBook
at all, otherwise you'd see the log and have an error thrown, I see none of those in the console
add a console.log above findById that just logs out something like "in updateBook"hm, that is very strange
I'm honestly not sure what's causing that then
np appreciate your help i will try to figure it out or look for a tutorial doing the same thing
Hopefully someone else will come along that does know 🙂 good luck!
i tried to put my code in a try catch block and it worked fine idk why i can't figure out
hm, the docs for mongoose claim it just returns
null
when it can't find the book, but I guess it's throwing after all
good to know you found a solution though!yep im just wondering how , i found a tutorial doing what i did exactly and works fine maybe something is new in moongose idk
yeah, could be a version issue
yep, any ways thanks for your time ❤️
haha, timezones
I would've told you this right away cause been there done that
always been that way, if you look for
1
thing and it's not there it throwsstupid, lying docs 😄
only time when it doesn't throw is if you look for more than one thing
findMany
and it's not there
because ofc it does
consistencyanother good reason I usually prefer just a DB driver over a ORM
haha funny you should mention that
that's just mongo
same thing happens in Go
so I'm guessing is how mongo works
so...another good reason to not use mongo 😎
haha, well, nice to see I can just move the column over without changing its header ;D
reason why I hate this so much is that now you have to consider the error if you want to do something specific in each case, because sure any error you can send a 404 but it isn't always a not found error...so if you wanted to show different messages you now have to check what error it is to send either a 404 or a 500
ok i got u but still can't know why it doesn't log book when its not found ?
because throwing an error stops code execution in the current context
hmm, but the log was before even checking if there is book or not
ok maybe because asynchandler of express resolve the await directly and throw the error ?
hm, I'm not sure
https://www.npmjs.com/package/express-async-handler
i think because it take a look here
npm
express-async-handler
Express Error Handler for Async Functions. Latest version: 1.2.0, last published: 2 years ago. Start using express-async-handler in your project by running
npm i express-async-handler
. There are 201 other projects in the npm registry using express-async-handler.asyncHandler catches the error automatically i think because of this it doesn't log