node js Array.push()无法使用mongoose工作

时间:2017-07-07 13:25:24

标签: javascript arrays mongoose nodes mean-stack

我是meanstack开发的初学者。我试图在foreach循环中推送数组内的值,但最后我得到空数组。

我的代码:

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

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

ourpackagesimages Schema

[
            {
                "_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
            }
    ]

预期产出

[
    {
        "_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
            }

        ]
    }
]

获得输出空

[]

2 个答案:

答案 0 :(得分:0)

您无法在同步函数内调用异步函数(在本例中为forEach)。结果在收集之前被访问,这就是空数组输出的原因。

为此目的,一个非常方便易用的库是async。对于您的特定用例,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);
            }
        });
    });
});

答案 1 :(得分:0)

简单地说, 声明一个数组     rest = [];

private void Label_MouseHover(object sender, EventArgs e)
{
    writeToInfoTxtBox("*****");
}

我希望它有所帮助,