Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a listener on Firebase value event?

How to properly set up a value event listener with Firebase, using Node.js?

I'm reading through Firebase's documents, and have successfully been able to update tables and rows on my Firebase database from both my Mac, and my Raspberry Pi running at the same time, connected to the same Firebase database; however, I'm not able to understand what I need to do with this segment for making a listener on a value event:

var starCountRef = firebase.database().ref('posts/' + postId + '/starCount');
starCountRef.on('value', function(snapshot) {
    updateStarCount(postElement, snapshot.val());
});

It's evident to me that I need somehow need to define postElement based on the output:

FIREBASE WARNING: Exception was thrown by user callback. ReferenceError: postElement is not defined

However, I have not idea what postElement is supposed to be, nor how to define it. Help!

I've made a very small program and table to simply update/change values. And I changed the program to I can see up console output when a row it changed.

This is my small user data variable, just so you now how little data I'm trying to change:

var user = {
    id:Number,
    name:String,
    number:Number
};

var users = [user];

And this is my change to the documentation code, so I can see something in the console output while Node.js is running:

var userRef = firebase.database().ref('/users/user' + users[0].id);
userRef.on('value', function(snapshot){
    firebase.app.updateUserInfo(postElement, snapshot.val());
    console.log('Users' + users[0].name + ' changed.');//<- this is the line I added.
});

NOTE: updateStarCount function is from the example in the Firebase documentation, meaningful to the example there, which I cautiously assume updates the local Firebase db...

NOTE2: If updateStarCount is doing some other magic, then it is a documentation flaw with Firebase, because the function isn't defined there. Any help clarifying this documentation would be greatly appreciated.

like image 770
NonCreature0714 Avatar asked Jan 05 '23 20:01

NonCreature0714


1 Answers

In order to make this question helpful for other people looking into same problem, here's an extended example from the question with missing functions and variables defined:

// some HTML element on the page
var postElement = document.getElementById("postElement");

// here I will assume that this function simply 
// updates the contents of the element with a value
var updateStarCount = function(element, value) {
    element.textContent = value;
};

var starCountRef = firebase.database().ref('posts/' + postId + '/starCount');
starCountRef.on('value', function(snapshot) {
    updateStarCount(postElement, snapshot.val());
});

With this JavaScript you can assume HTML page which looks like this:

<html>
  <body>
    <div id="postElement"></div>
  </body>
</html>
like image 50
Igor Nikolaev Avatar answered Jan 07 '23 18:01

Igor Nikolaev