我建立了一个MongoDB数据库,下面的函数将新的Cat对象添加到正确的集合中。但是,这两个操作发生的顺序与我期望的不同。虽然我希望先运行Cat.create({...}),然后再运行Cat.find(...),但我发现它实际上是相反发生的(例如Cat.find(... )先运行,然后再运行Cat.create({...}))。
任何可能的问题原因和修复建议,我们将不胜感激!
//add a new cat to the database
Cat.create({
name: "Sam",age: 1000,temperament: "Awesome"
},function(err,cat) {
if(err) {
console.log(err);
} else {
console.log("Recently added cat:")
console.log(cat);
}
});
//retrieve all cats from the DB and console.log each one
Cat.find({},cats) {
if(err) {
console.log("Error has occurred:");
console.log(err);
} else {
console.log("All the cats:");
console.log(cats);
}
});