简体   繁体   中英

getting error while connecting mongodb to node.js

I am getting error while trying to connect Mongodb and Nodejs on local server please any one help me with this problem

my JavaScript code

const mongodb = require("mongodb");

//provide the function sto connect to database
const MongoClient = mongodb.MongoClient;
const connectionURL = "mongodb://120.0.0.1:2701"
const databaseName = "task-manager"

MongoClient.connect(connectionURL,{useNewUrlParser:true},(error,client)=>{
if(error){
    return console.log(error)
}

console.log("connected to database")
})

here is the screenshot of error which I am facing

Replace your code with this code:

const connectionURL = “mongodb://120.0.0.1:27017”;

And add useUnifiedTopology:true with useNewUrlParser:true

The error says "connection time out", what means either your server is very slow or there is no mongoDB at the specified ip%port. If I assume that correctly that your mongoDB is running on localhost, that you have an typo in your connectionURL .You set it to 120 .0.0.1. It should probably be 127 .0.0.1.

const connectionURL = "mongodb://127.0.0.1:2701" or const connectionURL = "mongodb://localhost:2701"

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