mongoDB中的动态数据插入

时间:2018-08-24 06:36:58

标签: node.js mongodb

我正在将多个集合中的数据过滤到一个集合中,我编写了从多个集合中获取数据并将其插入到一个表中的代码,但是如果我动态声明插入数据,则会收到未处理的承诺拒绝错误。数组硬编码,然后代码可以正常工作。主要问题是在mongo集合中插入数据。请动态建议在收集数据时是否有人员伤亡。

    var data = {};
    // countryList001 - table which contains list of all countries.
    countryList001.countryFind(data,function(err,result){
        if(err){
            res.json({"status": "failed","message":err});
            return;
        }

        var cityDataList = [  
                                citydatalist0001, // tables which contains data of many countries(highlights of countries).
                                citydatalist0002
                            ];

        if(result.length > 0){
            asyncLoop(result,function(item,next){
            var data_search = {countryID:{$in:[countryId]}};
            asyncLoop(cityDataList,function(item1,next){
                //getting the results here.
                item1.findallData(data_search,function(err,resultByCountry){
                    if(resultByCountry.length > 0){
                        mySeperateTable.create(item_data);
                        //i have defined schema of mySeperateTable table. no issues with that.
                        //problem is, with this method i get warning 
                        //  Unhandled promise rejections are deprecated. In the future, 
                        //  promise rejections that are not handled will 
                        //  terminate the Node.js process with a non-zero exit code.

                        //but if i declare an array hard-code with the same values i am getting dynamically, this code will work.
                    }else{
                        console.log("length 0");
                        next();
                    }

                });

            },function(err){
                console.log(err);
                next();
            });

        },function(err){
            if(err){
                res.json({"status": "failed","message":err});
                return;
            }else{
                res.json({"status": "success","data":resultByCountry,"message":"finished"});
            }
        });
        }else{
            res.json({"status": "success","message":"No data found"});
            return; 
        }
    });

0 个答案:

没有答案