I am trying to get a json response from an API, when I do that call, I have to pass the user ID, which I get from the first request to then pass as parameter to the second request.
The problem is the order that things are running and that is what I do not understand. Could anyone explain that concept to me? Why does my first api request not happens before the
console.log("we got the id:"+id)
CODE:
app.get('/users/:name/info', function (req, res) {
var info= [];
var id;
var name = req.params.name;
console.log("now here: "+name); //that the first console.log I get
//request to get user id
var parametros = {search_string:name};
axo.Users.get(parametros, function(error, response){
var user;
console.log("should be here next"); //that is the third
for(let i = 0; i < response.data.length ; i++)
{
user = response.data;
console.log("id"+user[i].id);
//that is the fourth console.log
id = user[i].id;
}
});
//request to get user id
//request to get user information
console.log("we got the id:"+id);
//this returns undefined /second console.log
var params = {assigned_to_id:id};
axo.Features.get(params, function(error, response){
for(let i = 0; i < response.data.length; i++)
{
info = response.data;
}
res.contentType('application/json');
res.send(JSON.stringify(info));
});
//res.sendFile(path.join(__dirname + ("/index4-prototype.html")));
});