Next.js Discord

Discord Forum

Next.js API reconnecting to MongoDB

Unanswered
Brown bear posted this in #help-forum
Open in Discord
Brown bearOP
Hi.

I have a problem with Next.js and MongoDB. In general, everything works. The problem is when Next.js API and frontend starts before MongoDB starts. The API shows errors connecting to MongoDB (connect ECONNREFUSED 127.0.0.1:2710). If I kill Next.js and run npn run dev again, the connection to MongoDB works.

I use clientPrimise and try-catch according to:
https://www.mongodb.com/developer/languages/javascript/nextjs-with-mongodb/

How to force the API to try to reconnect to the DB after connectiona fail?

0 Replies