Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongdb TypeError: db.collection is not a function

When I run this code it has a Error 'TypeError: db.collection is not a function'. How to solve this problem and where is wrong in this code.

const MongoClient = require('mongodb').MongoClient;

// Connection url
const url = 'mongodb://localhost:27017/blog';

// Connect using MongoClient
MongoClient.connect(url, (err, db) => {
  const note = {
    text: 'Note content text',
    title: 'Note title'
  };
  
  db.collection('notes').insert(note, (err, result) => {
    if (err) {
      console.log('An error has occurred!')
    } else {
      console.log('Insert success!')
    }
  });
});
like image 295
Edwin Avatar asked Sep 13 '26 13:09

Edwin


1 Answers

It's mongodb version error which you have installed in your project. You should run npm install [email protected] --save in your project

like image 80
Taimoor Avatar answered Sep 15 '26 07:09

Taimoor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!