TypeError:db.close不是函数

时间:2018-05-29 09:18:57

标签: javascript node.js mongodb typeerror

我在下面使用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();
      });
    });
  });
});

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

TypeError: db.close is not a function

我无法理解为什么这不起作用。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:13)

正如@Neil Lunn所评论的那样,local function AddToTable(name, score) for k, v in pairs(scoretbl) do if v[1] == name then return false end end table.insert(scoretbl,{name, score}) end 应该用来代替client.close()