我从here阅读教程,我不明白为什么第二个“insertOne”不起作用。谢谢你的帮助!
var Promise=require('promise');
var MongoClient=require('mongodb').MongoClient;
var url = 'mongodb://localhost/EmployeeDB';
MongoClient.connect(url)
.then(function(db)
{
db.collection('Documents').insertOne({
Employeeid: 1,
Employee_Name: "Petro"})
.then(function(db1) {
db1.collection('Documents').insertOne({
Employeeid: 2,
Employee_Name: "Petra"})
})
db.close();
});
答案 0 :(得分:3)
您发生了两个异步操作(db.insertOne)。
因此,在第二次插入后,您应该有Matcher
并关闭连接
代码应如下所示
.then
答案 1 :(得分:0)
见评论
MongoClient.connect(url)
.then(function(db) {
// you need a return statement here
return db.collection('Documents').insertOne({
Employeeid: 1,
Employee_Name: "Petro"
})
.then(function(record) {
// another return statement
// try db instead of db1
return db.collection('Documents').insertOne({
Employeeid: 2,
Employee_Name: "Petra"
})
})
.then(function() {
// move the close here
db.close();
})
})
// Add an error handler
.then(null, function(error){
console.log(error)
})