I am unable to making connection to mongodb atlas cluster . i got the MongooseError which as following

Viewed 76

Error [MongooseError]: The uri parameter to openUri() must be a string, got "undefined". Make sure the first parameter to mongoose.connect() or mongoose.createConnection() is aongoose\lib\error\mongooseError.js:10:11) string.

const express = require("express");
const expressLayouts = require("express-ejs-layouts");
const mongoose = require("mongoose");
const MongoClient = require('mongodb').MongoClient;

const app = express();

// DB Config
 const db = require('./config/keys').MongoURI;



//Connect to Mongo
mongoose.connect(db, { useNewUrlParser: true, useCreateIndex: true ,useUnifiedTopology: true})
.then(() => console.log("MongoDB connected ...."))
.catch(err =>console.log(err));

const mongoose = require("mongoose");

    module.exports ={
      mognoURI:"mongodb+srv://pratham:<pratham>@psm-94d4p.mongodb.net/test?retryWrites=true&w=majority"
    };
1 Answers

You need to create a config folder and inside the config folder, you need to add keys.js file

which looks like this

module.exports = {
 MongoURI: 'here goes your connection string which you will get from MongoDB atlas'
 }
Related