0

I want to add object to the local variable inside promise but the variable is not accessible inside that, here's the code, please focus on the comments "accessible here and not accessible here":

        var dfs = [];

        var createDFs = function(item, index) {

          var country = item._id;
          item.country = (countries[0][country.toUpperCase()]);

          var findComps = Complete.find({'country': country});

          findComps.then(function(comps) {
            item.complete = comps;
            //dfs.push(item); //doesn't work here
          });

          dfs.push(item); //works here but item.comps not accessible

        };



        data.forEach(createDFs);
Faizan Ali
  • 973
  • 2
  • 16
  • 32
  • Probably `dfs.push(item);` is working but you are not seeing the items because the callbacks has not been executed yet. Why do you say that is not working? – Diego Jul 28 '17 at 07:14
  • try findComps.then((comps)=> { item.complete = comps; //dfs.push(item); //doesn't work here }); – Manas Jul 28 '17 at 07:18
  • @Mikethetechy, tried it and it doesn't work. – Faizan Ali Jul 28 '17 at 07:22

0 Answers0