MongoDB Atlas Error

When the Nextt.js page loaded i encountered this error and am quite confused on what is happening. Could this error be thrown because of errors in my .env.local file? Could anyone help?

Server Error

MongoParseError: Invalid scheme, expected connection string to start with “mongodb://” or “mongodb+srv://”

lib/mongodb.ts (21:13) @ eval

 19 | // is preserved across module reloads caused by HMR (Hot Module Replacement).
  20 | if (!global._mongoClientPromise) {
> 21 |   client = new MongoClient(uri, options);
     |           ^
  22 |   global._mongoClientPromise = client.connect();
  23 | }
  24 | clientPromise = global._mongoClientPromise;

Welcome to the MongoDB Community @Manny_N_A !

Per the error message, a valid MongoDB Connection String is expected to start with mongodb:// or mongodb+srv://.

You haven’t included a snippet showing how you are creating the connection, but if you are defining a MongoDB URI in your .env.local file it appears to be missing the expected prefix.

You may want to compare against the full example in How to Integrate MongoDB into your Next.js App.

A MongoDB Atlas connection string would look similar to the following as an environment variable:

MONGODB_URI=mongodb+srv://:@cluster0.mongodb.net/dbname?retryWrites=true&w=majority

Regards,
Stennie

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.