Skip to content

API resolved without sending a response (another one) #10777

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jordymeow opened this issue Mar 2, 2020 · 9 comments
Closed

API resolved without sending a response (another one) #10777

jordymeow opened this issue Mar 2, 2020 · 9 comments

Comments

@jordymeow
Copy link

I am not the only one with this issue, but other threads didn't fix my issue. I feel like there might be some issues in the library. Hopefully, it's simply in my code...

Describe the bug

I am using next-connect as well, as I need middlewares (I guess most of us need that library as well). I am always getting this warning:

API resolved without sending a response...

To Reproduce

const Handler = nextConnect();
Handler.use(middleware);

Handler.get(async (req, res) => {
  return new Promise(async resolve => {
    res.status(200).json({});
    return resolve();
  });
});

export default Handler;

I have simplified the code, of course, there are async calls in that function as well, otherwise, I wouldn't use an async function here ;) But this simple example doesn't work either.

I am currently using NextJS 9.2.1 and Next Connect 0.6.0.

@Janpot
Copy link
Contributor

Janpot commented Mar 2, 2020

Can't reproduce with

import nextConnect from "next-connect";
const handler = nextConnect();

handler.get(async (req, res) => {
  return new Promise(resolve => {
    res.status(200).json({});
    return resolve();
  });
});

export default handler;

You probably want to share a full reproduction , including the code for middleware.

@jordymeow
Copy link
Author

Thanks for your help @Janpot, but yes, the issue is linked to the middleware, and the fact NextJS is currently not completely compatible with next-connect.

There is a pull request for it: here.

Hopefully, this will be accepted, and more collaboration will be done with the next-connect library. I am not sure how it is possible to build a product based on NextJS without any middleware, or maybe there is a better alternative?

@naxmefy
Copy link

naxmefy commented Mar 21, 2020

The problem happens if you use another handler inside the handler

import nextConnect from 'next-connect'

const handler = nextConnect()

handler.use(nextConnect())
handler.get((req, res) => {
    res.status(204).end()
})

export default handler

image

it does not print the warning if you use only one handler (and add each middleware separately)

image

another example (with one handler)

image

@jordymeow
Copy link
Author

Exactly :) Personally I have stopped using next-connect, and I made my own middlewares. Fewer dependencies, and it's just as easy. That module is cool, but if NextJS doesn't care much about it, I prefer to build solutions avoiding it.

@naxmefy
Copy link

naxmefy commented Mar 21, 2020

Exactly :) Personally I have stopped using next-connect, and I made my own middlewares. Fewer dependencies, and it's just as easy. That module is cool, but if NextJS doesn't care much about it, I prefer to build solutions avoiding it.

may you give an example of how you do it?

@jordymeow
Copy link
Author

Actually, I don't do anything magical. I am just doing basically this: https://nextjs.org/docs/api-routes/api-middlewares. But I have created a little HOC for my API requests, and I basically add everything I need, like the DB, Auth, etc, and make sure those "modules" are singletons.

@hoangvvo
Copy link
Contributor

Hey, I am the author of next-connect. I just push the fix for this issue in 0.6.1

@jordymeow
Copy link
Author

Thank you so much @hoangvvo! :) I will definitely give it another try.

@balazsorban44
Copy link
Member

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 30, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants