“`const timeoutError = new error_1.MongoServerSelectionError(Server selection timed out after ${serverSelectionTimeoutMS} ms
, this.description);
.MongoServerSelectionError: connect ECONNREFUSED ::1:27017
const {MongoClient}=require('mongodb');
const url='mongodb://localhost:27017/';
const client= new MongoClient(url);
const dataBase= 'nodejs';
async function getdata(){
let result= await client.connect();
console.log('connect to server')
let db= result.db(dataBase)
console.log('2')
let collection = db.collection('node');
let response= await collection.find({}).toArray();
console.log(response)
}
getdata();
2
Answers
This happened probably because the MongoDB service isn’t started.
Follow the below steps to start it:
(in Windows the path will be:
Control PanelSystem and SecurityAdministrative Tools
)The server will start. Now execute
npm start
again and the code might work this time.The answer from a similar question (I can’t comment so I post as an answer)
Change uri to