0

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")));
  });

3 Answers 3

1

The functions execute asynchronously in Node.js, so the execution of one function does not wait for the other.

If you need it run one after the other, you can nest the functions in the order you require, with the outer one executing before inner one.

OR

you can use async await

Sign up to request clarification or add additional context in comments.

1 Comment

Have to look more deeply into async, await because I could not really implement that solution within my code. I used the second option nesting the functions. Thanks.
1

The function

axo.Users.get

is executed asynchronusly which means that it starts getting executed and the rest of the code continues it's execution before that function is done.

check this for more information Async Node.js

Comments

0

Get request is running asynchronously. It does not wait for your Get request to finish. Example : var fs = require("fs");fs.readFile('input.txt', function (err, data) { if (err){console.log(err.stack);return;} console.log(data.toString()); }); console.log("Program Ended");`
We have to use readFileSync . For more details

https://blog.risingstack.com/node-hero-async-programming-in-node-js/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.