简体   繁体   English

TypeError:db.close不是函数

[英]TypeError: db.close is not a function

I've the below Node.js application which uses MongoDb: 我在下面使用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();
      });
    });
  });
});

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? 有人可以帮忙吗?

正如@Neil Lunn所评论的那样,应该使用client.close()而不是db.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM