user3402639
user3402639

Reputation: 228

How to get the correct index after async call from Parse.com in Javascript/AngularJS

One of the keys in my Object-A Class is a pointer to another object-b, hence when I get an array of Object-As, I need to fetch the object-b (async) for each Object-A:

$scope.aList = queryResults;
for (var i = 0; i < queryResults.length; i++){ 
    getObject($scope.aList[i].pointerToB).then(function(objectB){
          $scope.aList[i].title = objectB.title;
    });
}

getObject: function(obj){
        return [$q.when(obj.fetch())];
},

even though all the objects are fetched successfully but they are all put in the wrong indexing, as you can see, the 'i' in $scope.aList[i].title is not in sync with the counter.

How can I correctly link the fetched object-b back to the original Object-A it belongs to?

Upvotes: 0

Views: 88

Answers (1)

Fosco
Fosco

Reputation: 38526

Even better... when you query for ObjectA class, use the include method so you don't need to fetch ObjectB objects embedded inside.

var query = new Parse.Query("ObjectA");
query.include("column-name-which-points-to-ObjectB");
query.find().then(function(results) {
  // ObjectB objects are fully fetched too.
});

Upvotes: 2

Related Questions