0

I am new to node js. I am working on simple user authentication app.

I am unable to connect to mongodb cluster0, getting error:

(node:22264) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor. TypeError: callback is not a function at $initialConnection.$initialConnection.then.err

I have used both ways by adding this or not { useUnifiedTopology: true } still getting same error

const express = require("express");
const expressLayouts = require("express-ejs-layouts");
const mongoose = require("mongoose");

const app = express();

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

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

My keys.js

module.exports = {
  MongoURI:
    "mongodb+srv://cluster0:<123455>@cluster0-7tt0p.mongodb.net/test?retryWrites=true&w=majority"
};
SuleymanSah
  • 17,153
  • 5
  • 33
  • 54
akacodes121
  • 129
  • 8

2 Answers2

0

You need to send useNewUrlParser and useUnifiedTopology in a single object.

{ useNewUrlParser: true, useUnifiedTopology: true }

Also be sure your connection string is correct, and loads from config correctly by console.log(db) after reading it.

SuleymanSah
  • 17,153
  • 5
  • 33
  • 54
0

You need to provide DBName in options if connecting to Atlas

mongoose

.connect(db,
    {dbName: 'yourDbName', 
     useNewUrlParser: true ,
     useUnifiedTopology: true }
).then(() => console.log('MongoDB Connected'))

.catch(err => console.log(err));

and all options in a single object. More Info here

Shivam
  • 3,514
  • 2
  • 13
  • 27