Hello guys I'm trying to use the values received from the first api url request as params of the second api url. Therefore, I've created a for loop and with the idea that for every element in the array I'm iterating will send it as the parameter (these are as you can see below the variables user, language, city).
But I think it's not working because for loop is synchronous and the module request from nodejs is asynchronous. However I can't figure out another way to do it...
request(firstUrl, function(err, res, body) {
let input = JSON.parse(body);
for( var i=0 ;i< input.length; i++){
var user= input[i]["user"]
var language= input[i]["language"]
var city= input[i]["city"]
var query_url= encodeURIComponent('{"data":{"call":{"data":[{"text":'+city+',"language":'+language+',"user":'+user+',"id":'+id+'}]}}}')
var secondUrl = {
url: `http://api.com/?request=${query_url}`,
method: 'GET',
headers: {
"Content-type": "application/json"
}
};
request(secondUrl, function(err, res, body) {
let input = JSON.parse(body);
console.log(err)
console.log(res)
console.log(input)
});
}
return mongo.multiSave(input.contents)
});