简体   繁体   中英

How use firebase startAt method with “value” and “key” in REST request?

In web use of firebase, I can use "value" and "key" for the "startAt" method, as demonstrated here:

https://stackoverflow.com/a/38024909/3466502

And documented here:

https://firebase.google.com/docs/reference/js/firebase.database.Reference?hl=pt-br#startAt

But I need this feature in a REST consume and I'm not finding any reference how I should do this.

Does anyone know how to do this?

I am using the PHP packages recommended by Firebase, but in their documentation I have not been able to get information on how I could do this ... :(

I have a data structure like this:

"list": {
    "-KsaZNyjd91tEAKjDffA": {"name": "a", "age" => 20},
    "-KsaZNynhTFuDBLpdmKv": {"name": "b", "age" => 21},
    "-KsaZNyoAoYAfHl-f6KF": {"name": "b", "age" => 22},
    "-KsaZiL4HJcoCYksHBEn": {"name": "b", "age" => 23},
    ...
}

I need to return two items per page dynamically. In JavaScript it was easy doing so:

var list = firebase.database().ref("list").orderByChild("name").limitToFirst(2);

var page_1 = $firebaseArray(list);
var page_2 = $firebaseArray(list.startAt('b', '-KsaZNyoAoYAfHl-f6KF'));

Using REST I created the following code:

$list = $db->getReference('list')->orderByChild('name')->limitToFirst(2);

$page_1 = $list->getValue();
$page_2 = $list->startAt('b', '-KsaZNyoAoYAfHl-f6KF')->getValue();
//                                      /
// "key" does not work in this method--/

But the "startAt" method of the package does not allow me to determine the "key" as the second parameter and thus the content of the second page is not what is expected.

Page 1

{"name": "a", "age" => 20},
{"name": "b", "age" => 21}

Page 2

{"name": "b", "age" => 21}, // <--- This is the last one on page one
{"name": "b", "age" => 22}  // <--- This should be the first on page 2

I looked at the PHP package method and it did not really implement anything for the "key". https://firebase-php.readthedocs.io/en/latest/realtime-database.html#startat

public function startAt($value): Query
    {
        return $this->withAddedFilter(new Filter\StartAt($value));
    }

So I looked at the firebase documentation for REST requests, but I also could not identify which parameter I should specify the "key" of my query. https://firebase.google.com/docs/database/rest/retrieve-data#consultas-de-intervalo

If you look at the code for startAt , it looks like that PHP library doesn't implement the second parameter.

Your best bet is to file an issue on the Github repo asking to support the second parameter to startAt() (and endAt() ), or to change the code yourself and file a Pull Request.

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