forEach 循环以错误的顺序返回一些项目

问题描述

我正在通过项目列表在我的 nodejs 路由中执行 foreach,然后在 foreach 完成后呈现 ejs 页面。但令我惊讶的是,我收到的一些商品的顺序是随机错误的。

router.get("/history",isLoggedIn,function(req,res) {
  var docs=[];
  Tube.find({}).sort({date: 'desc'}).limit(100).exec((err,tubes) => {



var itemsProcessed = 0;
        var status;

tubes.forEach(async (tube,index,array) => {
     await Box.findOne({ $or: [{"Tubes": tube._id},{"nok_Tubes": tube._id}]},async function (err,Box) {
      if(err || !Box) {console.log(err); res.json({message:"Failed"});}
      else{
        if(tube.status)
        status=await "ok";
        else if(tube.status===false) status=await "nok";
       await docs.push({qr: tube.qr_code,status,code_bar: Box.code_bar,insert_date: moment(tube.date).format("YYYY-M-D H:mm:ss a"),count_in_Box: tube.count_in_Box });
       }

    }).then(function () {
      itemsProcessed++;
      if(itemsProcessed === array.length) {
        console.log(docs[0]);
        res.render("detection/history",{docs});
      }
    });


});







   });
   
});

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)