简体   繁体   English

节点js Array.push()无法使用猫鼬

[英]node js Array.push() not working using mongoose

I am beginner in meanstack development. 我是meanstack开发的初学者。 I am trying to push value inside an array in foreach loop but last i am getting empty array. 我试图在foreach循环中将值推入数组内,但最后我得到的是空数组。

My code: 我的代码:

router.get("/", function(req, res) {

 ourpackage.find({}).sort({_id:1}).exec(function(err,resdoc){

  var rest = [];
  resdoc.forEach(function(doc){
    masterRoomimage.find({ourpackageId:doc._id},function(err,ourpackageimagevalue){
      if(err) res.send(err);
      var ourpackagedetail = JSON.parse(JSON.stringify(doc));
      var stringifyimages = JSON.parse(JSON.stringify(ourpackageimagevalue));
      var ourpackage = _.merge({},ourpackagedetail,{masterRoomimages:stringifyimages});
      rest.push(ourpackageimagevalue);
             //print all rest array value
             console.log(rest);
           });
  });
      //print empty rest array value
      console.log(rest);
      res.send(rest);
    });
});

ourpackage Schema ourpackage模式

  {
            "_id": "58e396d4215bc61338d2c06e",
            "first_title": "test 1",
            "berief_text": "<p>testing </p>",
   }

ourpackagesimages Schema ourpackagesimages模式

[
            {
                "_id": "59424d49fcc8100050916bf4",
                "imageLocation": "first.jpg",
                "ourpackageId": "58e396d4215bc61338d2c06e",
                "__v": 0
            },
            {
                "_id": "59424d49fcc8100050916bf5",
                "imageLocation": "third.jpg",
                "ourpackageId": "58e396d4215bc61338d2c077",
                "__v": 0
            },
            {
                "_id": "59490ad44e26c13324906433",
                "imageLocation": "second.jpg",
                "ourpackageId": "58e396d4215bc61338d2c06e",
                "__v": 0
            }
    ]

expected output 预期产量

[
    {
        "_id": "58e396d4215bc61338d2c06e",
        "first_title": "test 1",
        "berief_text": "<p>testing </p>",
        "ourpackagesimages": [
            {
                "_id": "59424d49fcc8100050916bf4",
                "imageLocation": "first.jpg",
                "ourpackageId": "58e396d4215bc61338d2c06e",
                "__v": 0
            },
            {
                "_id": "59490ad44e26c13324906433",
                "imageLocation": "second.jpg",
                "ourpackageId": "58e396d4215bc61338d2c06e",
                "__v": 0
            }

        ]
    }
]

obtained output empty 获得的输出为空

[]

You can't call an asynchronous function inside a synchronous function ( forEach in this case). 您不能在同步函数(在这种情况下为forEach中调用异步函数。 The result is accessed before it's collected and that's why the empty array output. 在收集结果之前先访问结果,这就是为什么空数组输出的原因。

A very handy and easy to use library for this purpose is async . 为此目的,非常方便且易于使用的库是async For your particular use case, map or mapLimit function will do. 对于您的特定用例,可以使用mapmapLimit函数。

var async = require('async');

router.get("/", function(req, res) {
    ourpackage.find({}).sort({
        _id: 1
    }).exec(function(err, resdoc) {

        async.mapLimit(resdoc, 5, function(doc, callback){
            masterRoomimage.find({
                ourpackageId: doc._id
            }, function(err, ourpackageimagevalue) {
                if (err) return callback(err);

                var ourpackagedetail = JSON.parse(JSON.stringify(doc));
                var stringifyimages = JSON.parse(JSON.stringify(ourpackageimagevalue));
                var ourpackage = _.merge({}, ourpackagedetail, {
                    masterRoomimages: stringifyimages
                });
                return callback(null, ourpackageimagevalue);
            });

        }, function(err, rest) {
            if(err){
                res.send(err);
            }
            else{
                res.send(rest);
            }
        });
    });
});

simply, Declare an array rest = []; 简单地,声明一个数组rest = [];

rest[] = ourpackageimagevalue;

I hope it helps, 希望对您有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM