简体   繁体   中英

Unable to connect to mongodb cluster

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"
};

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM