Hello, I have a problem that I have not found a solution yet.
I have an array of values, in which I make a scan and for each value execute function that makes a request to dynamodb
and returns a JSON. This request is asynchronous. With the value of each query, I want to populate an array, which will be returned via callback to my View
.
The problem is that when I give a push
in the Array, it returns me empty when I exit 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 somewhat similar to (a JSON Array):
{ apps: [{id: "value1", category: "category1"}, {id: "value1", category: "category1"}] }