Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: db.close is not a function

I've the below Node.js application which uses MongoDb:

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

var demoPerson = { name:'John', lastName:'Smyth' };
var findKey = { name: 'John' };

MongoClient.connect('mongodb://127.0.0.1:27017/demo', { useNewUrlParser: true }, function(err, client) {
  const db = client.db('demo');
  if(err) throw err;
  console.log('Successfully connected');
  //console.log(db);

  var collection = db.collection('people');
  collection.insert(demoPerson, function(err, docs) {
    console.log('Inserted', docs[0]);
    console.log('ID:', demoPerson._id);

    collection.find(findKey).toArray(function(err, results) {
      console.log('Found results:', results);

      collection.remove(findKey, function(err, results) {
        console.log('Deleted person');

        db.close();
      });
    });
  });
});

When I run it I get this error:

TypeError: db.close is not a function

I can't understand why this doesn't work. Can anybody help?

like image 322
runnerpaul Avatar asked May 29 '18 09:05

runnerpaul


1 Answers

As @Neil Lunn commented, client.close() should be used instead of db.close().

like image 197
runnerpaul Avatar answered Oct 08 '22 10:10

runnerpaul