Firebase:组合查询和子事件侦听器

Firebase:组合查询和子事件侦听器,firebase,firebase-realtime-database,nativescript,Firebase,Firebase Realtime Database,Nativescript,我有一个类似的结构 { posts : { post1Key: { title : "First Post", comments : { comment1Key : {}, comment2Key : {}, comment3Key : {} } } } ... } 我使用“/posts/post1Key/comments”键上的“child updated”事件来收听新的评论 如何

我有一个类似的结构

{
  posts : {
    post1Key: {
      title : "First Post",
      comments : {
        comment1Key : {},
        comment2Key : {},
        comment3Key : {}
      }
    }
  }
  ...
}
我使用“/posts/post1Key/comments”键上的“child updated”事件来收听新的评论

如何组合Firebase查询和事件侦听器


编辑:我知道这在Firebase中是可能的,但是,我使用的是您可以很好地将
查询
orderBy…
方法定义的方法和带
on()
方法的侦听器结合起来:请参阅详细文档

例如,你可以做如下事情

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

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

每次在“node1”下添加新的子节点时,您将获得按firstName排序的子节点列表,在编写本文时,这是不可能使用nativescript插件firebase的。解决方法是在收到数据后在eventHandler中进行排序/筛选

var onChildEvent = function(result) {
  //
  // Sort: result.value here
  //
};

// listen to changes in the /users path
firebase.addChildEventListener(onChildEvent, "/users").then(
  function(listenerWrapper) {
    var path = listenerWrapper.path;
    var listeners = listenerWrapper.listeners; // an Array of listeners added
    // you can store the wrapper somewhere to later call 'removeEventListeners'
  }
);

你能详细说明你的目标是什么吗?“组合Firebase查询和事件侦听器”的含义并不十分清楚。Firebase提供了两种从服务器读取数据的方法,一种是查询,另一种是使用事件。查询允许您进行排序和筛选等。据我所知,这些事件不允许。我的问题是,当我使用事件从服务器获取数据时,如何实现排序。谢谢您的回答。很抱歉,我遗漏了问题的一个关键部分,我已编辑并添加了该问题。我知道这在Firebase中是可能的,但是我正在通过插件(nativescript插件Firebase)在nativescript应用程序中使用Firebase,而这似乎根本不可能。@DavidLartey好的,我无法帮助您。。。。我很想删除我的答案,因为它不能回答您的问题,但我想我不会,因为它可能会帮助正在研究如何“将查询和子事件侦听器结合起来”(您的问题标题)的人