db.open(function(err,dbcont){
console.log("in open");
if(err){
console.log(err);
db.close();
}
// console.log(dbcont);
dbcont.collection('things',function(err,collection){
console.log("in collection");
if(err){
console.log("open collecion failed");
}
//console.log(collection.find());
collection.insert({xx:123},{safe:false});
var cr= collection.find();//.toArray(function(err,docs){
console.log(cr);
cr.each(function(err,docs){
console.log("in find to array");
if(err){
console.log("to Array Error");
}
console.log(docs);
});
// console.log(rs);
});
console.log("ready to close");
res.end("open succeed");
db.close();
});
open和db.collection的回调都执行了,为什么collection.find().toArray()的回调不执行呢。也就是console.log(docs)没有显示,而且 console.log(“in find to array”);这句也没执行。和网上的例子差不多啊,不好使呢?
1 回复
上边那个是改用each方法后的代码。使用toArray的代码在这里
db.open(function(err,dbcont){
console.log("in open");
if(err){
console.log(err);
db.close();
}
// console.log(dbcont);
dbcont.collection('things',function(err,collection){
console.log("in collection");
if(err){
console.log("open collecion failed");
}
//console.log(collection.find());
collection.insert({xx:123},{safe:false});
var cr= collection.find({}).toArray(function(err,docs){
console.log("in find to array");
if(err){
console.log("to Array Error");
}
console.log(docs[0]);
});
// console.log(rs);
});
console.log("ready to close");
res.end("open succeed");
db.close();
});