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?
As @Neil Lunn commented, client.close()
should be used instead of db.close()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With