• 1
name Punditsdkoslkdosdkoskdo

Firebase: combine query and child event listener

I have a structure similar to this

  posts : {
    post1Key: {
      title : "First Post",
      comments : {
        comment1Key : {},
        comment2Key : {},
        comment3Key : {}

And I'm using the "child updated" events on the key "/posts/post1Key/comments" to listen for new comments.

How can I combine Firebase Query and the event listeners.

Edit: I know this is possible with Firebase, however, I'm using nativescript-plugin-firebase

You can very well combine a Query defined with an orderBy... method and a listener with the on() method: see detailed doc here

For example you could do something like

var query = db.ref('node1').orderByChild('firstName');

query.on('value', function(dataSnapshot) {
  dataSnapshot.forEach(function(childSnapshot) {
    var childKey = childSnapshot.key;
    var childData = childSnapshot.val();

Each time a new sub node is added under "node1" you will get the children list ordered by firstName

  • 0
Reply Report