we have the following factory to retrieve data from firebase:
.factory('UserInfo', ["$firebase", "$q",
function($firebase, $q) {
//initialize firebase
var ref = new Firebase("https://xxx.firebaseio.com/users/data");
var dataRef = $firebase(ref).$asArray();
//$q for synchronous method call
var deferred = $q.defer();
var promise = deferred.promise;
return {
getProfile: function(ID) {
console.log(ID);
dataRef.$loaded()
.then(function(data) {
var record = data.$getRecord(ID);
var profileData = {
"profileID": record.profileID,
"displayName": record.displayName,
"email": record.email,
"picture": record.picture,
"birthdate": record.birthdate,
"age": record.age,
"hobby": record.hobby,
"gender": record.gender,
"firstname": record.firstname,
"lastname": record.lastname,
"numberHuggs": record.numberHuggs,
"rating": record.rating
};
//console.log(profileData);
deferred.resolve(profileData);
//return profileData;
}) // end then
.catch(function(error) {
console.error("Error getting UserInfo:", error);
deferred.reject("Error getting UserInfo: " + error)
}); // end catch
return deferred.promise;
} // end function(ID)
};
} // end function
]) //end factory
in our controller, we access this factory using
$scope.chatList.$loaded().then(function() {
for (var i = 0; i < ($scope.chatList).length; i++) {
console.log($scope.chatList[i].otherProfileID);
UserInfo.getProfile($scope.chatList[i].otherProfileID).then(function(value) {
console.log(value);
});
};
});
essentially, it takes profileIDs of users, sends them to UserInfo.getProfile() and getProfile then returns an object with our data. sadly this doesn't work. the console.log() in our controller code indicate, that different profileIDs are handled in the factory, but in the end, it returns (console.log(value)) the first userprofile 3 times (we have 3 different profileIDs in the array) any hints?