1

I am trying to call an api after an initial call returns the parent objects, so that I can set some child properties. The parent objects are days and they own timeslots. So in angular I am watching the days array and then I try to fetch each day's timeslots.

However somehow restangular assigns duplicate timeslot results to different days. When I check the chrome logs I can see that the correct requests were made to the right endpoint but the results are duplicated, eg a request for tuesday's timeslots may return wednesday timeslots, so tuesday and wednesday end up having the same timeslots. Here is my code:

$scope.$watch(function() {
  return vm.days;
}, function(newvalue, oldvalue) {
  for (var c = 0; c < newvalue.length; c++) {
    const index = c;
    newvalue[index].getList("timeslots").then(function(result) {
      console.log("index: " + index + ", c:" + c);
      newvalue[index].timeslots = result;
    }, function(response) {
      console.log(response);
    });
  }
}, false);
  • I don't see anything wrong with the code. What's the output you are getting? If `getList` is returning wrong values, it might be the problem not your call. – Bergi Jun 18 '16 at 17:19
  • I really have no idea what could be wrong, because from browser logs the request is made with the correct id but the response returned is for a different resource. When I do the same request via postman it returns the correct data. My suspicion is that it could be a problem with using promises in the loop, other answers dealing with chaining request suggest using array.reduce but I want my requests to be parallel – Japheth Ongeri - inkalimeva Jun 18 '16 at 21:05
  • So what is the output you are getting from all these logs? In what browser are you executing this, does it support block-scoped `const`? – Bergi Jun 18 '16 at 21:09
  • I am testing in google chrome, the log of index changes as expected and the variable c always outputs the last value 5. Restangular then makes a request to the right endpoint like days/3/timeslots but gets a response for a different day, eg for days/1/timeslots. The same request works correctly in postman – Japheth Ongeri - inkalimeva Jun 19 '16 at 07:34
  • 1
    Sounds like the API doesn't handle parallel requests correctly. Can test such with postman? Maybe post the serverside code. – Bergi Jun 19 '16 at 10:58
  • Ok thanks @Bergi , I will look into that and maybe post another question if it is an issue. – Japheth Ongeri - inkalimeva Jun 19 '16 at 21:04

1 Answers1

0

After encountering this problem again in a different project I realised that it is an API problem, specifically to do with bean scope for the rest endpoint. I solved it by adding the spring annotation @Scope("prototype") annotation to the jersey rest endpoint class. Check out my other answer on this question for more details.

Community
  • 1
  • 1