0

I have such a problem (structured english)

GET HTTP Resource
   FOR every data item received do 
       GET another HTTP Resource
       Alter the original data from the outer loop with data from the inner GET
RETURN altered data

How would you pass the outer data to the inner request, e.g. does not work (in pseudo code):

GET HTTP (callback function (recDataOUTER){
    GET NEW HTTP ((recDataOUTER, recDataINNER){
        Alter both data accordingly
    })
return altered data
})

Is there a more elegant way? The background is that I am using MongoDB and am struggling with Joins (there are no joins I know).

1 Answer 1

1

To verify, you are making a GET request. This request is returning a list of IDs. You then want to loop through those IDs and make a GET request for each of those ids. You then want to pass the data to the parent scope. Is this correct?

$http(...).success(function(data) {
  data.forEach(data, function(value, key) {
    $http(...).success(function(childData) {
      //Save childData to parent data object
      data[key] = childData.whatever;
    });
  })
});

In this situation you should be able to simply alter the data variable directly from within the child HTTP callback function above. You shouldn't have any scope issues within the child callback function.

As an aside, it is usually best practice to request all of the data in a single call, rather than making a series of GET calls. I don't know the specifics of your project here, but you may reconsider your approach.

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

2 Comments

You say before the last closing bracket block (last line), I can do a return data or print data and it will work?
Nope, that will not work. Remember, each $http call is asynchronous, so if you return after the first $http success function, the children $http calls will not have completed.

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.