简体   繁体   中英

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?

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 . Turns out orderByKey() is for sorting purposes only. 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. Hope this helps :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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