-1

I am not able to get value after for loop scope in node.js

for (var i = 0; i < project_list[0].KeywordsId.length; i++) {
    keywordModel.find({
        _id: project_list[0].KeywordsId[i]
    }, function(err, keywords) {
        keywordsArray.push(keywords[0].text);
    });
}
console.log(">>>>>>>>>>>>>>>>>>>>>>", keywordsArray);
user3871566
  • 51
  • 1
  • 2
  • 6
  • possible duplicate of [How to return the response from an Ajax call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – jgillich Jul 29 '14 at 11:59

1 Answers1

0

you have to write the asyncronous code to perform this action. in your case it will be like this

https://github.com/caolan/async#map

var async = require('async'),
  keywords;

async.map(project_list[0].KeywordsId, 
   function(id, cb){
    keywordModel.find({ _id: id }, cb);
}, function(error, keywordsFound){
  if(error){
    throw error; 
  } else {
    keywords = keywordsFound;
  }
});


setTimeout(function(){
//we have to wait for keywords to be found  
console.log(keywords);
}, 1000);
vodolaz095
  • 6,680
  • 4
  • 27
  • 42