0

I have created a code in node.js, which use mongoDB. Everything is working fine, but when I use nested loop, it break and don't return the proper values.

DB1.find({},function(error,fetchAllDB1) {
    var mainArray = new Array();
    for (var i in fetchAllDB1) {
        if(fetchAllDB1[i].name) {
            var array1  = new Array();
            var array2  = new Array();

            array1['name'] = fetchAllDB1[i].name;
            array1['logo'] = fetchAllDB1[i].logo;
            array1['desc'] = fetchAllDB1[i].desc;    

            DB2.find({is_featured:'1', brand_id: fetchAllDB1[i]._id}, function(error,fetchDB2) {
                for (var p in fetchDB2) {
                    var pArr=[];
                    pArr['_id']    = fetchDB2[p]._id;
                    pArr['name']   = fetchDB2[p].name;
                    pArr['sku']    = fetchDB2[p].sku;
                    pArr['description'] = fetchDB2[p].description;

                    console.log('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>');
                    console.log(pArr);
                    console.log('<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<');
                    array2[p]=pArr;
                } 
                array1['pp']= array2;
            });
            mainArray[i]=array1;
        }
    }
    console.log('######### LAST #########');
    console.log(mainArray);
    console.log('######### LAST #########');
});

In my console message its showing

 console.log('######### LAST #########');

  All Values

  console.log('######### LAST #########');

After that

console.log('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>');
All Values;
console.log('<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<');

I want to use async in my code, so that all data fetching occur as below:

mainarray =
[
    [array1] = All Values
    [array2] = [0]
               [1]  
]

mainarray =
[
    [array1] = All Values
    [array2] = [0]
               [1]  
]
DrakaSAN
  • 7,673
  • 7
  • 52
  • 94
Pritesh Mahajan
  • 4,974
  • 8
  • 39
  • 64
  • How does your code even work? I mean `for (var i in fetchAllDB1)` the `i` should be an object but you use it as an integer? – Stan Aug 23 '16 at 14:49
  • adding to @Stan 's comment, what is DB2? the collection or the actual db? If it's the DB, what are you querying for? I don't see in the docs that the db has a find method... – Pedro Otero Aug 23 '16 at 14:54
  • Anyway, what you are looking for is this library, there you can make asynchronous actions in loops. https://caolan.github.io/async/ – Stan Aug 23 '16 at 14:58
  • Don't use async (advice). Use promises instead. Your code will be much more clear and easily debugable. It's true that it will require a bit effort to handle it if you didn't use promises before, but probably not much than what will be required to fix and maintain async based approach. – bitifet Aug 23 '16 at 15:57

1 Answers1

-1

Your code is very confusing and overly complicated.

What you should do is (psuedo code):

DB1.find(foo) // returns one result
    .then(getFromDB2);

function getFromDB2 (res){
    return DB2.find(res); // make a query here with mongo drivers `$in`
}

Mongodb : $in operator vs lot of single queries

Community
  • 1
  • 1
Patrick Motard
  • 2,650
  • 2
  • 14
  • 23