question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
katowulfcommented, Mar 10, 2014

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);
0reactions
katowulfcommented, Oct 26, 2015

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Firebase .on('value') not working - Stack Overflow
Actually firebase's database has it's authentication and I found out that the rules on my database is not set properly (to public).
Read more >
Read and Write Data on the Web | Firebase Realtime Database
If the transaction is rejected, the server returns the current value to the client, which runs the transaction again with the updated value....
Read more >
Realtime Database - React Native Firebase
Data is stored as JSON and synchronized in realtime to every connected client. React Native Firebase provides native integration with the Android &...
Read more >
Adding data | Firestore - Google Cloud
Use the setDoc() method: import { doc, setDoc } from "firebase/firestore"; ... Update the timestamp field with the value from the server
Read more >
Using Firebase - Expo Documentation
Firebase JS SDK does not support all services for mobile apps. Some of these services are Analytics, Dynamic Links and Crashlytics. See the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found