Express sets body to Cannot GET [route] while asynchronous code is being executed #4965
Unanswered
askenderski
asked this question in
Q&A
Replies: 1 comment
-
res.body is undefined until it is actually sent and is not present in the get method. The HTML Cannot GET /articles comes from a different request (e.g. browser auto-requesting favicon.ico or hitting a non-matching route), not from your route itself. To fix itLog before sending JSON directly: const articles = await ArticleModel.find();
console.log("Sending:", articles);
res.status(200).json(articles); Move logging into your middleware after res.end: res.end = function (chunk) {
if (chunk) chunks.push(Buffer.from(chunk));
res.body = Buffer.concat(chunks).toString('utf8');
console.log("Final response body:", res.body);
oldEnd.apply(res, arguments);
}; |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I have an express application with some articles stored in a Mongo database. While I'm waiting for the mongoose model Article to load, the body of the request is changed to:
and the status code to 404. The code I'm using to see the body being sent is this:
It's a slightly modified version of this one: https://stackoverflow.com/a/34712950/13300387
My code is this:
route
model
I have posted this same question on Stack Overflow (https://stackoverflow.com/questions/73237206/express-sets-body-to-cannot-get-route-while-asynchronous-code-is-being-execute) but I did not get an answer and cannot think of how to modify it to be more concise or clear.
Beta Was this translation helpful? Give feedback.
All reactions