在forEach循环之后,数组变为null

时间:2016-07-20 10:52:47

标签: arrays angularjs node.js mongodb push

我有以下代码将数据插入mongo db,

 var obj=[];
    ThirdNavigation.find(searchApp).exec(function(err, app) {
                if (err) return done(err);

                app.forEach(function(navi){
                    if(typeof navi.variants == "undefined"){
                        ApplicationInventory.findOrCreate({id :navi.id},navi).exec(function(e,update){
                            if(e) console.log(e);
                            var query = {
                                productId: navi.id,
                                appId: navi.appId,
                                childId: navi.childId,
                                name: navi.name,
                                price: navi.price,
                                quantity: navi.quantity
                            }
                            PriceAndVariants.findOrCreate({productId : navi.id},query).exec(function(err,variants){
                                if(err) console.log(err);
                                if(variants != ""){
                                    update.variant = variants[0];
                                    obj.push(update);
                                }
                                else{
                                    obj.push(update);
                                }
                            })
                        })
                    }
                    else{
                    var jData = JSON.parse(navi.variants);
                        for(var i=0; i< jData.length; i++){

                            navi.price = jData[i].price;
                            navi.quantity = jData[i].quantity;
                            navi.sku = jData[i].sku;
                            navi.discount = jData[i].discount;

                            ApplicationInventory.findOrCreate({id :navi.id},navi).exec(function(e,update){
                                if(e) console.log(e);
                                var query = {
                                    productId: navi.id,
                                    appId: navi.appId,
                                    childId: navi.childId,
                                    name: navi.name,
                                    price: navi.price,
                                    quantity: navi.quantity
                                }
                                PriceAndVariants.findOrCreate({productId : navi.id},query).exec(function(err,variants){
                                    if(err) console.log(err);
//                                    console.log(update);
                                    if(variants != ""){
                                        update.variant = variants[0];
                                        obj.push(update);
                                    }
                                    else{
                                        obj.push(update);
                                    }
                                    console.log(obj);
                                })
                            })
                        }
                    }
                })
                console.log(obj);
                                    res.send(obj);
 });

但obj变为null,我从前端收到一个空数组。但是当我在forEach中打印obj时,数据打印得很完美。我找不到错误。

0 个答案:

没有答案