Next.js Discord

Discord Forum

App Router and MongoDB

Unanswered
Gouty oak gall posted this in #help-forum
Open in Discord
Gouty oak gallOP
I have two apps, one with pages and one with app router. Both use the same code for connecting to Mongo.

if (process.env.NODE_ENV === 'development') {
  // In development mode, use a global variable so that the value
  // is preserved across module reloads caused by HMR (Hot Module Replacement).
  if (!global._mongoClientPromise) {
    client = new MongoClient(uri, options);
    global._mongoClientPromise = client.connect();
  }
  clientPromise = global._mongoClientPromise;
} else {
  // In production mode, it's best to not use a global variable.
  client = new MongoClient(uri, options);
  clientPromise = client.connect();
}

// Export a module-scoped MongoClient promise. By doing this in a
// separate module, the client can be shared across functions.
export default clientPromise;


The pages one has no issue. The app router I see a lot of these errors in the log:

Unhandled Promise Rejection     {"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"MongoNetworkTimeoutError: connection timed out","reason":{"errorType":"MongoNetworkTimeoutError"


Any ideas why app router would behave differently in this respect?

0 Replies