skip to Main Content
var MongoClient = require('mongodb').MongoClient;
var url = 'mongodb://localhost:27017/mydb'

MongoClient.connect(url, function(err,db) {
    if (err) {
        console.log("err")
    } else {
        console.log("Database Connected")
    }
})

.connect is striked off in VS Code

err is displayed in the VS Code terminal

Node.js node-v18.12.0-x64
Mongodb version 4.2
windows 8.1 Pro

2

Answers


  1. Sometimes it happens to me also. I don’t use the connect function for now (macOS). Try this one:

    const client = new MongoClient(uri);
    const db = client.db('defaultDB');
    

    and use it like:
    await db.collection('movies').countDocuments();

    Login or Signup to reply.
  2. Use this code. I had the same error and now it works (found this solution from this article on official mongoDb website).

    MongoClient.connect('mongodb://0.0.0.0:27017',{ useNewUrlParser: true ,useUnifiedTopology:true},function(err,connect) {
      if(err){
        console.log("Error!")
      } else {
        console.log('Database connected.')
      }
    })
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search