I'm having trouble with repeating data from a loop (which is not accurate, the data should be unique). I believe the problem is due to a poor implementation/understanding of promises.
var posts = PostsData.getPosts();
$scope.$watch($scope.active, function() {
$timeout(function() {
var markers = [];
for (var key in posts) {
var post = posts[key];
if (posts.hasOwnProperty(key) && posts[key]!=null) {
var p = $q.defer();
p = gf.get(key).then(function(location) {
console.log(post.title)
return ({
idKey: key,
title: post.title,
coords: {
latitude: location[0],
longitude: location[1]
}
});
});
markers.push(p);
}
}
$q.all(markers).then(function(markers) {
$scope.markers = markers;
});
});
})
}
$scope.markers is filled with repeating post.title data. Any help would be greatly appreciated. I'm new to programming, so I apologize if my issues seem simple.