如何使用具有“值”的firebase startAt方法和“钥匙”在REST请求? [英] How use firebase startAt method with "value" and "key" in REST request?

查看:163
本文介绍了如何使用具有“值”的firebase startAt方法和“钥匙”在REST请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在web使用firebase的时候,我可以使用startAt方法的value和key,如下所示:

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



记录在这里:

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



但是我在REST消耗中需要这个特性,我没有找到任何参考,我应该怎么做。

有谁知道该怎么做这是什么?



我正在使用Firebase推荐的PHP软件包,但是在他们的文档中,我一直无法获取有关如何执行此操作的信息... :(

我有这样的数据结构:

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

我需要返回两个项目每页动态。在JavaScript中,这样做很简单:
$ b $ $ $ $ $ $ p $ var $ list = firebase.database().ref(list)。orderByChild 名称)limitToFirst(2)。

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

使用REST我创建了以下代码:

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

$ page_1 = $ list-> getValue();
$ page_2 = $ list-> startAt('b','-KsaZNyoAoYAfHl-f6KF') - > getValue();
//
//key在此方法中不起作用 - /

但是包的startAt方法不允许我把key确定为第二个参数,因此第二个页面的内容不是预期的。

Page 1

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

Page 2

  {name:b,age=> 21},//< ---这是第一页的最后一个
{name:b,age=> 22} //< ---这应该是第2页的第一个

我看了PHP的包装方法,并没有真正实现钥匙的任何东西。 https://firebase-php.readthedocs.io/en/ ($ value):query
{
return $ this-> withAddedFilter(new Filter \ StartAt($ value));





$ b

所以我查看了REST请求的firebase文档,但是我也无法确定哪个参数我应该指定我的查询的关键。 https://firebase.google.com/docs/数据库/休息/检索数据#consultas-de-intervalo

解决方案

=https://github.com/kreait/firebase-php/blob/master/src/Firebase/Database/Query.php#L152 =nofollow noreferrer> startAt的代码,看起来像该PHP库不实现第二个参数。



您最好的选择是提交问题在Github回购要求支持第二个参数 startAt()(和 endAt() ),或自行更改代码并提交合并请求。


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.

这篇关于如何使用具有“值”的firebase startAt方法和“钥匙”在REST请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆