Error while trying to connect and insert data from nodeJS mongoDB driver.

15 views
Skip to first unread message

Kuldeep kumar pandey

unread,
Nov 5, 2019, 12:21:21 AM11/5/19
to mongodb-user
I am trying to insert data in the MongoDB database, I am using the following code: 


const url = "mongodb://localhost:27017";
const db_name = "course-app";

const insert_user = (data) => {
    const client = new MongoClient(url);

    client.connect(function(err, client) {
        assert.equal(null, err);
        console.log("Connected correctly to server");

        const db = client.db(db_name);

        db.collection('users').insertOne(...data, (err, result) => {
            console.log("Data inserted");
            client.close();
        })
    })
}

But I am getting the following error: 
TypeError: client.connect is not a function

I can't understand this error message, I have searched a lot on google and can't find any solution. 
I am a newbie with mongoDB, and I am following the instructions given at mongoDB documantation, please help me regarding this. Why is this error? ,
    is this a problem in the mongoDB driver itself?, 
    is there any other way to connect with mongoDB database using nodeJS. 



Kevin Adistambha

unread,
Nov 5, 2019, 7:08:32 PM11/5/19
to mongodb-user

Hi,

Are you using the latest MongoDB node driver? Latest is version 3.3.3.

You might be using an outdated version of the driver that predates the change that MongoClient.connect() returns a connection object.

Best regards,
Kevin

Kuldeep kumar pandey

unread,
Nov 5, 2019, 11:41:59 PM11/5/19
to mongod...@googlegroups.com
Yeah, I am using the latest version. 
After a lot of time, finally, I found the solution. I need to pass  { useUnifiedTopology: true }, to the mongoclient constructor.  

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/dd87211c-7c45-4d9a-861a-c63b435ed3eb%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages