next.js: False positive report of "API resolved without sending a response"
Bug report
Describe the bug
Api endpoint
Next will log the “API resolved without sending a response” warning even though the API endpoint always send a response back.
To Reproduce
Here the code that triggers the warning:
export default async function(req: NextApiRequest, res: NextApiResponse) {
switch (req.method) {
case "POST":
case "OPTION": {
try {
const request = http.request( // Node core http module
url,
{
headers: await makeHeaders(req, res),
method: req.method,
},
response => {
response.pipe(res);
},
);
request.write(JSON.stringify(req.body));
return request.end();
} catch (error) {
Log.error(error); // Can be a simple console.error too
}
return res.status(500).end();
}
}
return res.status(405).end();
}
It seems that this endpoint will always send a response back.
Expected behavior
No warning
System information
- OS: MacOS
- Version of Next.js: 9.2.1
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 12
- Comments: 28 (10 by maintainers)
Commits related to this issue
- feat: add upload notion links In order i have done these things with this commit: 1. Fix svg import creating a custom d.ts module and adding includes in tscondig.json 2. Add interfaces and models for... — committed to cartabinaria/note-website by deleted user 5 years ago
- feat: add upload notion links In order i have done these things with this commit: 1. Fix svg import creating a custom d.ts module and adding includes in tscondig.json 2. Add interfaces and models for... — committed to cartabinaria/note-website by deleted user 5 years ago
- feat(api): add basic mongo api (#9) * feat: add basic mongo API Created courses and sections api entries with NEXT js. Doesn't have custom filtering or queries integrated with Mongo, should make ... — committed to cartabinaria/note-website by Flecart 5 years ago
@gaku-sei it is most likely caused by the
callbackas we can’t detect you are still doing work in the API unless you return a promise and wait to resolve it until after the callback is fired e.g.This should solve your case, but you can now disable the warning also: https://nextjs.org/docs/api-routes/api-middlewares
I see the same warning with
apollo-server-express.@iddify please provide code instead of saying “same issue”, it’s unlikely that you’re doing 100% exactly the same as the person that posts an issue.
Looks like you’re not finishing the request when a body is not set:
Should be:
I had the same issue with the following code:
What solved it for me was
awaiting both handlers, like so:await handlePost(req, res).I’m having the same problem. Why is this issue closed? Is this a problem from Next server or am I doing something wrong?
I havent been able to figure it out yet. Im considering to switch to express server to handle all requests. (Instead of just api)
@timneutkens Thank you for the quick reply. Unfortunately
9.2.2-canary.12doesn’t solve the issue 😞I don’t know exactly how Next analyzes whether or not a route actually returns something, but it could be because of the callback in
http.requestor thetry/catch?I also wonder if this couldn’t be because of the
http.requestfunction that could “silently” fail and just not do anything 🤔I will investigate a bit more 👍