AngularJS: Issue with http chain calling and NodeJS: issue with using variable in multiple http call

Viewed 206

I have a get call and a post call in my node.js file, both of which use the same variable that I initialized to an empty string outside these calls. In the post call, I set the variable, while in the get call, I return the value of the variable to my clientside angularjs that is requesting the value. In my angularjs file, I make the post call first and then the get call, which means the value should be set and should be available when the get call is issued and returns. Here's what I'm doing:

NodeJS

var myUrl= "";
app.post('/post', function(req, res){
   myUrl = res.url;
}); 

app.get('/get, function(req, res){
    res.json({result: myUrl});
});

AngularJS:

var promise = $http.post('/post')
        .then(function(response){

            return $http.get('/get');

        }).then(function(response){
            console.log(response.data.result);
        });

I've tried AngularJS promise chain calling but it still doesn't work. The problem I'm having is that when I make the get call in the first round of requests, the url variable hasn't been set yet even though the post call has been already issued, so the get call returns an empty string. In the second round of requests, the get call returns the value that has been set from the first post call, and so on and so forth.

Any ideas on why this is happening and suggestions on how to solve this issue so that the get call returns the value that is set in the post call in the same round of requests (the get call is issued when the post call is done)? I'm fairly new to NodeJS so any help is appreciated!

1 Answers
Related