NextJs .env File problem: MongooseError: The `uri` parameter to `openUri()` must be a string, got "undefined". Make sure the first parameter to `mongoose.connect()` or `mongoose.createConnection()` is a string

Hey Guys,
from one day to another my .env File seems to have an issue…
I have published the exact same code on vercel and defined the same Environment Variables and it works without a problem.

The message i am getting:

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

my env file:

MONGODB_URI =
  'mongodb+srv://XXX:XXXX@cluster0.tvglm.mongodb.net/XXXX?retryWrites=true&w=majority';

My db.js file:


import mongoose from 'mongoose';

const connection = {};

async function connect() {

  if (connection.isConnected) {

    console.log('already connected');

    return;

  }

  if (mongoose.connections.length > 0) {

    connection.isConnected = mongoose.connections[0].readyState;

    if (connection.isConnected === 1) {

      console.log('use previous connection');

      return;

    }

    await mongoose.disconnect();

  }

  const db = await mongoose.connect(process.env.MONGODB_URI);

  console.log('new connection');

  connection.isConnected = db.connections[0].readyState;

}

async function disconnect() {

  if (connection.isConnected) {

    if (process.env.NODE_ENV === 'production') {

      await mongoose.disconnect();

      connection.isConnected = false;

    } else {

      console.log('not disconnected');

    }

  }

}

function convertDocToObj(doc) {

  doc._id = doc._id.toString();

  doc.createdAt = doc.createdAt.toString();

  doc.updatedAt = doc.updatedAt.toString();

  return doc;

}

const db = { connect, disconnect, convertDocToObj };

export default db;

i hope someone can point me in the right direction…
Thanks

Don’t you need an import or require to use variables from .env file?

Examples I have seen look like:

require('dotenv').config();

process.env.USER_ID // "239482"
process.env.USER_KEY // "foobar"
process.env.NODE_ENV // "development"

See https://nodejs.dev/learn/how-to-read-environment-variables-from-nodejs

1 Like

Hi there,

thanks for your reply. I have seen this aswell. However its not necessary… since my Live-Page with the same Code is working right now.
…Even weirder: Yesterday it suddenly worked… now it doesnt work anymore (talking about npm run dev only…)
my live page is living happily ever after…

I followed the following tutorial to set it up:
youtube-Tutorial

Do you maybe have another idea what the reason might be ?

EDIT:

I now found out that when i paste my MONGODB_URI string directly into my db.js file… so it says:

  const db = await mongoose.connect('mongodb+srv://XXX:XXXX@cluster0.tvglm.mongodb.net/XXXX?retryWrites=true&w=majority');

then it works fine… so maybe i need to find a different workaround to get the string correctly?..
I cant get the “dotenv” module to work … can you give me another hint where i would add the “require…” ?

… When i console.log(process.env.MONGODB_URI) just before the original line; i am getting my MONGODB_URI Link as a string: “…”
i dont understand why it cant use it in the next line right after it :smiley:

thanks a lot in advance

1 Like

If you look at the examples from the link I posted. You will see that the value is within double quotes and on the same line. I would try that.

alright, so i managed to solve it. Dont ask me why it works like that now…

All i had to do: take the “” away

Thanks for your help!

1 Like

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