为什么我的NodeJS / Mongoose代码没有按页面上的代码顺序运行?

我建立了一个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);
    }
});
fangguifenshiwodidi 回答:为什么我的NodeJS / Mongoose代码没有按页面上的代码顺序运行?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3091379.html

大家都在问