当由孩子排序时使用startAt()进行分页 [英] Pagination using startAt() when ordering by child
问题描述
我试图只检索记录列表,按每个记录中的一个字段排序,我希望能够检索页面中的数据。
根据 Firebase文档, reference.startAt()
方法有一个可选的第二个参数,它是:
子钥匙开始。这个参数只允许按照子,值或者优先顺序排序。
{
products:{
-KlsqFgVWwUrA-j0VsZS:{
name:产品4,
价格:666
},
-Klst-cLSckuwAuNAJF8:{
name:Product 1,
price :
$,
-Klst7IINdt8YeMmauRz:{
name:Product 2,
price:50
},
-Klst9KfM2QWp8kXrOlR:{
name:Product 6,
price:30
},
-KlstB51ap1L2tcK8cL6:{
名称:Product 5,
price:99
},
-KlstDR5cCayGH0XKtZ0:{
name:Product 3,
价格:500
}
}
}
这里是能够检索到的代码Page 1(最低价格商品+第二低价格+第三低价格):
(I使用Firebase JS SDK 4.1.1)
'use strict';
var firebase = require('firebase');
firebase.initializeApp({
apiKey:your-api-key,
authDomain:your-firebase-domain,
databaseURL:https: //your-db.firebaseio.com,
projectId:your-project,
storageBucket:your-bucket.appspot.com,
messagingSenderId:your-sender- (b)
$ b $ firebase.database()。ref('products')
.orderByChild('price')
.limitToFirst(3)
.on('child_added',function(snapshot){
var key = snapshot.key;
var data = snapshot.val();
console.log(key +': '+ JSON.stringify(data))
})
输出:
$ b $ TJ:database tjwoon $ node test.js
-Klst9KfM2QWp8kXrOlR:{name :Product 6,price:30}
-Klst7IINdt8YeMmauRz:{name:Product 2,price:50}
-KlstB51ap1L2tcK8cL6:{name:Product 5,price:99}
第2页应该是第3低,第4低,第五低价产品cts,这意味着我的代码需要一个额外的行:
$ $ $ $ $ c $ firebase.database()。ref('products')
.orderByChild('price')
.startAt(null,'-KlstB51ap1L2tcK8cL6')
.limitToFirst(3)
.on('child_added',function(snapshot){
var key = snapshot.key;
var data = snapshot.val();
console.log(key +':'+ JSON.stringify(data))
})
输出:
$ $ $ $ $ $ $ $ $ $ $ $ $ TJWO $ $ test.js
-Klst9KfM2QWp8kXrOlR:{ name:Product 6,price:30}
-Klst7IINdt8YeMmauRz:{name:Product 2,price:50}
-KlstB51ap1L2tcK8cL6:{name产品5,价格:99}
问题在于它再次返回页1。如果文档是正确的,结果应该从 -KlstB51ap1L2tcK8cL6
记录开始。
一个 .indexOn
规则为价格字段,但结果保持相同。
如果我删除<$ c $ b> orderByChild()行,那么结果是从给定的键开始的,当然排序是不正确的,加上它的行为与文档相反...
我发现这些其他堆栈溢出帖子描述了同样的问题: 但是这些问题没有答案,反应也很少。 Github存储库中没有匹配搜索词 是否有其他人面临相同的问题?这个问题使得以分页的方式检索一个排序列表是不可能的。 p> 问题在于如何为第二页调用
$ b startat
的问题。
startAt
:
.startAt(null,'-KlstB51ap1L2tcK8cL6')
<要获得正确的结果,您需要传递所谓的锚项的价格和关键字:
.startAt(99,'-KlstB51ap1L2tcK8cL6')
通过Firebase可以找到 price
99
,然后返回从' - KlstB51ap1L2tcK8cL6'
开始的项目。
I am trying to just retrieve a list of records, sorted by one of the fields in each record, and I would like to be able to retrieve the data in pages.
According to the Firebase documentation, the reference.startAt()
method has an optional 2nd parameter, which is:
The child key to start at. This argument is only allowed if ordering by child, value, or priority.
First off, here is the data:
{
"products" : {
"-KlsqFgVWwUrA-j0VsZS" : {
"name" : "Product 4",
"price" : 666
},
"-Klst-cLSckuwAuNAJF8" : {
"name" : "Product 1",
"price" : 100
},
"-Klst7IINdt8YeMmauRz" : {
"name" : "Product 2",
"price" : 50
},
"-Klst9KfM2QWp8kXrOlR" : {
"name" : "Product 6",
"price" : 30
},
"-KlstB51ap1L2tcK8cL6" : {
"name" : "Product 5",
"price" : 99
},
"-KlstDR5cCayGH0XKtZ0" : {
"name" : "Product 3",
"price" : 500
}
}
}
And here is the code which is able to retrieve Page 1 (lowest priced item + 2nd lowest priced + 3rd lowest priced):
(I am using Firebase JS SDK 4.1.1)
'use strict';
var firebase = require('firebase');
firebase.initializeApp({
apiKey: "your-api-key",
authDomain: "your-firebase-domain",
databaseURL: "https://your-db.firebaseio.com",
projectId: "your-project",
storageBucket: "your-bucket.appspot.com",
messagingSenderId: "your-sender-id"
})
firebase.database().ref('products')
.orderByChild('price')
.limitToFirst(3)
.on('child_added', function (snapshot) {
var key = snapshot.key;
var data = snapshot.val();
console.log(key + ': ' + JSON.stringify(data))
})
Output:
TJ:database tjwoon$ node test.js
-Klst9KfM2QWp8kXrOlR: {"name":"Product 6","price":30}
-Klst7IINdt8YeMmauRz: {"name":"Product 2","price":50}
-KlstB51ap1L2tcK8cL6: {"name":"Product 5","price":99}
Page 2 should be the 3rd lowest, 4th lowest, and 5th lowest priced products, which means my code need one additional line:
firebase.database().ref('products')
.orderByChild('price')
.startAt(null, '-KlstB51ap1L2tcK8cL6')
.limitToFirst(3)
.on('child_added', function (snapshot) {
var key = snapshot.key;
var data = snapshot.val();
console.log(key + ': ' + JSON.stringify(data))
})
Output:
TJ:database tjwoon$ node test.js
-Klst9KfM2QWp8kXrOlR: {"name":"Product 6","price":30}
-Klst7IINdt8YeMmauRz: {"name":"Product 2","price":50}
-KlstB51ap1L2tcK8cL6: {"name":"Product 5","price":99}
The problem is that it is returning Page 1 again. If the documentation is correct, the results should start at the record with key -KlstB51ap1L2tcK8cL6
.
I have tried adding an .indexOn
rule for the price field, but the results remained identical.
If I remove the orderByChild()
line, then the results do start from the given key, but of course the sorting is incorrect, plus it behaves contrary to the documentation...
I found these other Stack Overflow posts which describe the same problem:
- Firebase orderByChild with startAt()'s second argument w/ pagination not odering
- (see comment below. I don't have enough reputation to include the link here)
However there are no answers to those questions and quite few responses. There are no issues in the Github repository which match the search term startat
.
Is anyone else facing the same issue? This problem makes it impossible to retrieve a sorted list in a paginated manner...
You're almost there!
The problem is in how you call startAt
for the second page:
.startAt(null, '-KlstB51ap1L2tcK8cL6')
To get the correct results you need to pass in both the price and the key of the so-called anchor item:
.startAt(99, '-KlstB51ap1L2tcK8cL6')
With that Firebase will find all items with price
99
and then return items starting at key '-KlstB51ap1L2tcK8cL6'
.
这篇关于当由孩子排序时使用startAt()进行分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!