Categories
JavaScript Answers

How to fix TypeError: db.collection is not a function with Node MongoDB?

Spread the love

To fix TypeError: db.collection is not a function with Node MongoDB, we should make sure we’re getting the collection from the database object.

For instance, we write

MongoClient.connect(db.url, (err, database) => {
  const myAwesomeDB = database.db("myDatabaseNameAsAString");
  myAwesomeDB.collection("theCollectionIwantToAccess");
});

to call connect to connect to the database.

And we get the database from the database.db method.

We then get the collection from the database’s collection property.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published.