3
Hello, I have a problem I haven’t found a solution.
I have an array of values, in which I scan and for each value run function that makes a request to dynamodb
and returns a JSON. This request is asynchronous. With the value of each query, I want to fill an array, which will be returned via callback for my View
.
The problem is, when I give one push
in Array, it returns me empty when leaving the scope of my asynchronous function.
exports.appCategory = function(ids, callback1){
var result = { apps: []};
async.forEachSeries(ids, function(item, cb){
async.series([
function(callback){
//funcao assincrona
verify(item.id, function(res){
result.apps.push(res);
});
callback();
},
]);
cb();
});
console.log(result);
}
My database query function:
function verify(id, callback){
scrapeDB.getItem(id, function(response){
var category;
url = "https://play.google.com/store/apps/details?id="+id+"&hl=pt";
if(response === null){
request(url, function(error, response, html){
if(error){
console.log(error);
return false;
}else{
var $ = cheerio.load(html);
if(response.statusCode == "404"){
category = "Outros";
}else{
$('.category').filter(function(){
var data = $(this);
dataCategory = data.children().first().text();
category = dataCategory;
});
}//else
//save data
scrapeDB.putItem(id, category, function(resp){
callback(resp);
//return resp;
});
}//else
});//request
}else{
callback(response);
//return response;
}
});//scrapedb
}//function verify()
My current result is:
{ apps: [] }
The expected result is something similar to (a JSON array):
{ apps: [{id: "value1", category: "category1"}, {id: "value1", category: "category1"}] }
Where do you use that
async.forEachSeries
? in the repository of async.js I can’t find that method...– Sergio
Hi @Sergio, I found this link: http://stackoverflow.com/questions/15942832/nodejs-async-foreachseries-execution-order
– user3319937