Mongoose returns 502

PLEASE help us help you by writing a good post!

  • we need to know your netlify site name. Example:
  • DNS issues? Tell us the custom domain, tell us the error message! We can’t help if we don’t know your domain.
  • Build problems? Link or paste the FULL build log & build settings screenshot

The better the post - the faster the answer.
The netlify site is
importing mongoose leads to a 502 error…
it fails to return the response despite connecting to the database… and performing the crud operations successfully

Hiya @martini.daniels and welcome to our community!

Are you sure your site is a Jamstack site? Sounds like you are expecting a very different kind of web hosting than we provide - check out this site for some more details on our hosting philosophy:

I think you may be using a function to do this, which may work on our service, but just needed to clarify that our non-function hosting wouldn’t run code for you at browse time.

If I’m correct, could you tell me the x-nf-request-id from a 502’ing request so I can look into it in our internal logs? Sounds like you might be trying to run for more than 10 seconds which will not work by default, but I’ll be able to tell for sure with this detail:

The x-nf-request-id header is c27e3f33-5e47-487e-b9e3-6e4aea37a353-25993604
Its a nextjs app. deployed using next-on-netlify and target is serverless…

Hi, @martini.daniels. I did confirm that this 502 response was caused by the function not completing within the 10 seconds allowed.

Often this happens because a promise is waiting on a connection to be closed and because the connection is left open the callback never triggers. This means the database action might complete but still not trigger a callback.

I don’t know if that is what is happening with this function or not, but this is the most common root cause I see. If this is the root cause, making sure to manually close database (or HTTP) connections is a solution which works for many people.

The next step will be to troubleshoot the function code itself to find out why it doesn’t return a response within the 10 seconds. You might do this by using console.log() at various steps in the code to see where it “gets stuck”.

​Please let us know if there are other questions about this.