Skip to content Skip to sidebar Skip to footer

Angularfire - $firebasearray Can $add But Not Read

I feel like I must be missing something very basic. Here is a portion of my service: angular.module('fire') .constant('FIREBASE_URI', 'https://___.firebaseio.com/') .factory('

Solution 1:

If you look at $add $firebaseArray, It does create new item & add it into to $firebaseArray as like we have buildings. But as soon as you add item to ``$firebaseArray` it doesn't get added instantly. It get added when the $add promise get resolved.

I think you are doing correct thing, only you need call syncArraySvc.addBuilding(item) method on success of $add promise.

To make this approach you need to return a promise from the service method like

var addBuilding = function(item) {
  return buildings.$add(item);
};

And then the caller function will take that promise and on resolve of it, he will call syncArraySvc.addBuilding(item) method that have assurity that items has added in buildings array.

syncArraySvc.addBuilding({foo: "bar"}).then(function(addedItem){
    console.log(addedItem);
    console.log(syncArraySvc.addBuilding(item)); //this will show you updated list
})

Solution 2:

The other answers helped get me pointed in the right direction.

The API documentation has a code sample that doesn't seem to need the data to be wrapped in a promise:

varlist = $firebaseArray(new Firebase(URL));
$scope.list = list;

However, it does point out that you can use the $loaded promise to be notified when the data is loaded. This is how I got it to work in my project:

syncArraySvc.getBuildings().$loaded(function(data) {
  $scope.buildings = data;
});

I tried replicating this in a fresh project, and it consistently worked without the $loaded wrapper, like they show in the first example. It makes sense to me that the $loaded wrapper would be required. I don't understand how it could be working in the first example without it.

Solution 3:

Try using a $timeout service inside the getBuildings function or rather when you call it. It probably takes a little while before data is returned.

Post a Comment for "Angularfire - $firebasearray Can $add But Not Read"