【问题标题】:Firebase retrieve chid dataFirebase 检索 chid 数据
【发布时间】:2016-10-13 09:58:24
【问题描述】:

我的对象如下

  • -通知
    • 用户ID
      • 键1
        • 数据键1
          • 日期:10-10-2016
          • 时间:下午 3:20
          • 状态:打开
      • 键2
        • 数据键2
          • 日期:11-10-2016
          • 时间:下午 5:00
          • 状态:关闭

在 Notifications/userId 之前我有参考路径

如何检索 status='open' 的数据

来自 cmets 的代码:

ref.orderByChild("status")
   .equalTo('open')
   .on("value", function(snapshot) { console.log(snapshot.key); }); 

【问题讨论】:

  • ref.orderByChild("status").equalTo('open').on("value", function(snapshot) { console.log(snapshot.key); });

标签: javascript firebase firebase-realtime-database


【解决方案1】:

Firebase 查询只能包含一个动态键。

在您的情况下,key1/key2datakey1/datakey2 下有两个动态键。您要么必须将其中一个放在固定位置(例如key1/my/statuskey2/my/status),要么必须以不同的方式构建数据。

notificationsStatusPerUser
    userId: {
      key1_datakey1: {
        status: "open",
        path: "key1/datakey1"
      }
      key2_datakey2: {
        status: "close",
        path: "key2/datakey2"
      }
    }
}

现在您可以通过以下方式查找特定用户的所有未清项:

var query = ref.child('notificationsStatusPerUser')
               .child(uid)
               .orderByChild(status)
               .equalTo('open');
query.on('child_added', function(snapshot) {
    ref.child('notifications')
       .child(uid)
       .child(snapshot.val().path)
       .once('value', function(linkedSnapshot) {
           console.log(linkedSnapshot.val());
       });
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-07
    • 1970-01-01
    • 1970-01-01
    • 2016-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多