angularfire icon indicating copy to clipboard operation
angularfire copied to clipboard

Using $firebase().$on('value', ...) does NOT work

Open skingsland opened this issue 12 years ago • 10 comments

I have 2 types of entities stored in Firebase, Cars and Users. Each car has a userID, which is the identity of the user who owns the car. I have a web page where I display data about the car, and I want to display the name of the car's owner. This means that I need to sync both the car and the user name to the $scope, so I can display both sets of data on the same web page.

I was previously getting the car and user data using Firebase refs and once('value', ...) calls, which works just fine:

// get a Firebase reference to the car
var carRef = ref.child('cars').child($scope.carID);
// ...and bind the car to the $scope, so I can display it's data (not shown)

// now use the car's userID to look up the user's name, and bind it to the $scope
carRef.child('userID').once('value', function(userID) {
    // use the userID to look up the user, and bind her data to the $scope
    // (again not shown, but works just fine)
});

Now I'm trying to do the same thing using the new $firebase() API, which I've read about in the new API docs, angularFire-seed project, StackOverflow questions and even online tutorials. But it's not working for me like it should:

var carRef = ref.child('cars').child($scope.carID);

// get a $firebase reference to the car
var $car = $firebase(carRef);

// now get the $car's userID, so we can look up the user
$car.$child('userID').$on('value', function(userID) {
    // this is never called!
});

Based on the firebase API documentation, calling $on('value', ...) should work just as well as calling on('value', ...) or once('value', ...) (as I did above). But the 'value' event's callback is never called! If I change 'value' to 'loaded' then it does in fact work, but only the first time. If I view another car's page (a different angular route) and then come back, it doesn't work. Only if I do a full page refresh does the 'loaded' event's callback get called again. Which actually makes complete sense, given the docs for loaded.

Anyway, why isn't this working as expected?

Here are the versions I'm using:

  • firebase.js 1.0.5
  • firebase-simple-login.js 1.2.5
  • angular.js 1.2.14
  • angular-route.js 1.2.14
  • angularfire.js 0.7.0

skingsland avatar Mar 07 '14 04:03 skingsland

We just closed this issue a few hours ago. In the upcoming release. 1, 2, 3

katowulf avatar Mar 07 '14 15:03 katowulf

Yeah, it appears to be fixed now. But I'm going to stick with using once('value', ...) because it's not clear to me how I'm supposed to use $on('value', ...) to do a one-time lookup. After the event fires the first time, I'd have to call $off('value', ...) right away so I don't get another event, that triggers another data binding.

Is there any plan to have a $once() method, for one-time angularfire data lookups?

skingsland avatar Mar 09 '14 01:03 skingsland

@skingsland the $on method returns a promise which resolves with an off method. So this would work for a one time lookup:

$on('value', function(snap) {
     console.log( snap.val() );
}).then(function(unbind) { unbind(); });

katowulf avatar Mar 10 '14 15:03 katowulf

Thanks for info. I'm closing this because the bug is fixed in the latest version on master.

skingsland avatar Mar 10 '14 15:03 skingsland

Junk, that was incorrect. I think I took one too many bugs to the head this morning. (The $bind() method resolves with an unbind method, not $on).

So the correct usage would be as you stated it:

var callback = function(snap) {
   console.log(snap.val());
   ref.$off('value', callback);
};

ref.$on('value', callback);

katowulf avatar Mar 10 '14 17:03 katowulf

Yeah, honestly I think it's simpler/cleaner to use the firebase once() function instead, especially since I can just call $getRef() to get the Firebase object:

var ref = $firebase(...);

ref.$getRef().once('value', function(snap) {
    console.log(snap.val());
};

skingsland avatar Mar 10 '14 17:03 skingsland

Keep in mind that when using once() you'll need to trigger a $apply yourself:

var ref = $firebase(...);
ref.$getRef().once('value', function(snap) {
    $timeout(function() {
       $scope.var = snap.val();
    });

     /* this is a little less flexible but preferred by some:
         $scope.var = snap.val();
         $scope.$apply();
     */
};

katowulf avatar Mar 10 '14 17:03 katowulf

Thanks, I appreciate you giving me a heads up on that. When I want to pull data from Firebase and set it on the scope, I use $bind() instead so I don't have to worry about triggering $scope.$apply(). In this scenario, I don't actually need the value until later.

My use case for calling once('value', ...) is when I need a value right now (i.e. synchronously). This comes up with foreign keys (or whatever they're calling in Firebase)... in my example above, I want to display information about a car, so I bind the car to my $scope. But the car has a userID, that I need to use to look up information about the user who owns the car. The car and user data can be asynchronously synced with the model, because they aren't actually needed until render time. But I need to know the value of car.userID right away in the controller, so I can set up the data binding to sync the user's data with the model, so I can show the user's name, address, etc. in the view instead of just the userID value.

Unless you see a better way of setting up the user binding?

skingsland avatar Mar 10 '14 23:03 skingsland

All data reads from Firebase are asynchronous. .once calls your callback when the data is available. On 11/03/2014 9:31 am, "Steve Kingsland" [email protected] wrote:

Thanks, I appreciate you giving me a heads up on that. When I want to pull data from Firebase and set it on the scope, I use $bind() instead so I don't have to worry about triggering $scope.$apply(). In this scenario, I don't actually need the value until later.

My use case for calling once('value', ...) is when I need a value right now (i.e. synchronously). This comes up with foreign keys (or whatever they're calling in Firebase)... in my example above, I want to display information about a car, so I bind the car to my $scope. But the car has a userID, that I need to use to look up information about the user who owns the car. The car and user data can be asynchronously synced with the model, because they aren't actually needed until render time. But I need to know the value of car.userID right away in the controller, so I can set up the data binding to sync the user's data with the model, so I can show the user's name, address, etc. in the view instead of just the userIDvalue.

Unless you see a better way of setting up the user binding?

Reply to this email directly or view it on GitHubhttps://github.com/firebase/angularFire/issues/272#issuecomment-37247671 .

johnsoftek avatar Mar 10 '14 23:03 johnsoftek

@romelgomez your comments have been deleted. This thread is over a year old, your comments were unrelated to the original question, and your comments were unrelated to AngularFire. If you want to discuss, reach out on firebase-talk or submit a new issue.

katowulf avatar Oct 26 '15 14:10 katowulf