简体   繁体   English

Firebase如何在用户数据下动态搜索推送键

[英]Firebase how to dynamically search for a push key under a user's data

{
    "foo":{
        "uid":{ // user's uid
            "push_key_1":{
               "bar":"baz"
            },
            "push_key_2":{
               "bar":"baz"
            }
        }
    }
}

Given the code model above, how do I dynamically search for the push_key_1 or push_key_2 in a query? 鉴于上面的代码模型,如何在查询中动态搜索push_key_1push_key_2

I've tried to do : 我试过这样做:

var searKey = 'push_key_1';

db.ref('foo').orderByKey().equalTo(searKey).once('value')
    .then(function(snap){
        // snap.val() returns null.
    }).catch();

But the snap.val() is null . snap.val()null Turns out orderByKey() is for sorting purposes only. 结果orderByKey()仅用于排序目的。 Is there a way to achieve this? 有没有办法实现这个目标?

It's a lot simpler than what you're trying. 这比你正在尝试的要简单得多。 When you know the key of the node you want to load, you don't need a query at all: 当您知道要加载的节点的密钥时,根本不需要查询:

db.ref('foo').child(searKey).once('value'....

I think it's not possible unless you restructure the database. 我认为除非你重组数据库,否则这是不可能的。 My approach is to add another node to store the push keys. 我的方法是添加另一个节点来存储推送键。

"users_push_keys": {
    "push_key_1": "uid_1",
    "push_key_2": "uid_1"
}

With this, you can get the uid by retrieving the data from this node. 有了这个,您可以通过从此节点检索数据来获取uid。 Hope this helps :) 希望这可以帮助 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM