Home > Blockchain >  What does mongodb is not a function mean?
What does mongodb is not a function mean?

Time:09-24

I'm new to programming, I'm trying to follow a web developemnt course when connecting to database I created I get the error "mongodb.connect is not a function".

I try to connect to the database using the following code.

        let mongodb = require('mongodb');
        let db
        let connectionString = '';
        mongodb.connect(connectionString,{useNewUrlParser: true, useUnifiedTopology: 
        true},function(err, client){
        db = client.db();

CodePudding user response:

You can connect to the database from the client (which uses MongoClient from the library).

const { MongoClient } = require('mongodb');

let db;

const connectionString = 'mongodb://' // Change this to your uri
const client = new MongoClient(connectionString)

Then you can connect

await client.connect();
db = client.db();

CodePudding user response:

Try this:

const { MongoClient } = require("mongodb");

let databaseName = '';
let connectionString = '';

MongoClient.connect(connectionString, {useNewUrlParser: true, useUnifiedTopology: true}, (error, client) => {
  if (error) 
    return console.log("Connection failed for some reason");

  const db = client.db(databaseName);
});
  • Related