TypeError:db.close不是函数

run*_*aul 10 javascript typeerror mongodb node.js

我在下面使用MongoDb的Node.js应用程序:

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();
      });
    });
  });
});
Run Code Online (Sandbox Code Playgroud)

当我运行它时,我收到此错误:

TypeError: db.close is not a function
Run Code Online (Sandbox Code Playgroud)

我不明白为什么这不起作用.有人可以帮忙吗?

run*_*aul 13

正如@Neil Lunn评论的那样,client.close()应该用来代替db.close().