Hunter Scott Lacefield
Hunter Scott Lacefield

Reputation: 125

the `uri` parameter to `openUri()` must be a string, got "undefined" MongooseError

I am trying to begin mongoDB and mongoose challenges on freecodecamp and want to answer through a deployed web app from heroku. I have successfully completed the first challenged via my heroku app that connects mongoose to database. But when I try to run the code locally it returns with this error.

throw new MongooseError('The `uri` parameter to `openUri()` must be a ' +

MongooseError: The `uri` parameter to `openUri()` must be a string, got "undefined".

Also, the app doesn't complete challenge two which is to create a model in which the code is written correctly as I found the answer online to make sure that wasn't the problem. I think it is because of this error and I have no idea how to fix it. Please help me 👍🏻.

This is myApp.js

require("dotenv").config();
const mongoose = require("mongoose"); // Need to require mongoose
//** 1) Install and set up mongoose. (connected it to heroku as well.)
mongoose.connect(process.env.MONGO_URI, {
  // The MONGO_URI string is in sample.env. Be sure to change <password> to the user's actual password for mongoose to connect to the database
  useNewUrlParser: true,
  useUnifiedTopology: true,
});

here is my sample.env file (yes it is saved in the root part with package.json and myApp.js)

MONGO_URI="mongodb+srv://password2:[email protected]/myFirstDatabase?retryWrites=true&w=majority"

Upvotes: 0

Views: 2088

Answers (1)

singhAmandeep007
singhAmandeep007

Reputation: 499

You need not to enclose MongoURI in quotes to identify it as string . You should remove quoutes around it.

MONGO_URI=mongodb+srv://password2:[email protected]/myFirstDatabase?retryWrites=true&w=majority

Also make sure :

    const dotenv = require('dotenv');

    dotenv.config({ path: './sample.env' });

Upvotes: 1

Related Questions