skip to Main Content

This is the error I am getting:

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. did not connect

My .env file in server folder:

MONGO_URL='mongodb+srv://dummyuser:Abhinav@[email protected]/?retryWrites=true&w=majority'
PORT=3001

My index.js file in server folder:

import express from "express";
import bodyParser from "body-parser";
import mongoose from "mongoose";
import cors from "cors";
import dotenv from "dotenv";
import multer from "multer";
import helmet from "helmet";
import morgan from "morgan";
import path from "path";
import { fileURLToPath } from "url";

/* CONFIGURATIONS */
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
dotenv.config();
const app = express();
app.use(express.json());
app.use(helmet());
app.use(helmet.crossOriginResourcePolicy({ policy: "cross-origin" }));
app.use(morgan("common"));
app.use(bodyParser.json({ limit: "30mb", extended: true }));
app.use(bodyParser.urlencoded({ limit: "30mb", extended: true }));
app.use(cors());
app.use("/assets", express.static(path.join(__dirname, "public/assets")));

/* FILE STORAGE */
const storage = multer.diskStorage({
    destination: function (req, file, cb) {
      cb(null, "public/assets");
    },
    filename: function (req, file, cb) {
      cb(null, file.originalname);
    },
  });
  const upload = multer({ storage });

/* MONGOOSE SETUP */
const PORT = process.env.PORT || 6001;
mongoose
  .connect(process.env.MONGO_URL, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
  })
  .then(() => {
    app.listen(PORT, () => console.log(`Server Port: ${PORT}`));
  })
  .catch((error) => console.log(`${error} did not connect`));

2

Answers


  1. The error message suggests that the process.env.MONGO_URL is undefined, which means that the value of the MONGO_URL key in your .env file is not being properly loaded into the environment variables.

    To debug this issue, you can try:

    Make sure that your .env file is located in the root directory of your project.
    Double-check that the key MONGO_URL is correctly spelled and has the correct value in your .env file.
    Add console.log(process.env.MONGO_URL) before the mongoose.connect() function to check if the environment variable is being properly loaded.
    Make sure you have installed and configured the dotenv package correctly.
    If you’re running your app in a production environment, ensure that you have set the MONGO_URL environment variable properly.

    Login or Signup to reply.
  2. There is a Chance that may be you have entered the wrong details.

    You should check the followings fields in Your Mongo_URI:

    Mongo_URL= "mongodb + srv:// USER: [email protected]/DATABASENAME? retryWrites = true & w = majority"
    
    1. your user
    2. your password
    3. your cluster name
    4. your database name

    You should check these things on the mongodbAtlas website, after logging in.

    If any of these are wrong change them.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search