火力地堡ENDAT()时髦的行为与orderByChild()进行分页时/无限滚动
我想首先加载用户最近的帖子和页面它们按降序排列,但我使用endAt().limitToLast()
与orderByChild()
有麻烦。
我可以将网页中的项目使用精细与startAt().limitToFirst()
但orderByChild()
我需要有从最终名单负荷......当执行使用endAt()
的orderByChild()我的查询,似乎被忽略。
下面是我对categories
节点JSON
{
"-LY8EYaWHINB1khsIEvJ" : {
"Recipies" : true,
"username" : "User1"
},
"-LYDaIrnDKIWndMcLE-g" : {
"Recipies" : true,
"username" : "User4"
},
"-LY8Em4B6COFk3how5FC" : {
"Buds" : true,
"username" : "User2"
},
"-LY8Eq2E1muFcOBstODa" : {...},
"-LY8Esi98QdhszIgvRRN" : {...},
"-LY9OSc7u8wTNQaJ7BXL" : {...},
"-LY8EymPGxK8Y_YnRfC0" : {...},
"-LY8F0RrYbLNPpYwIuMX" : {...},
"-LY8F3QfERAhOq3iW3jC" : {...},
}
继承人什么我的查询看起来像(注意我需要从下往上取此):
const fetchCategoryImages = (category, currentImages, lastKey) => {
if (!lastKey) {
return () => {
firebase.database().ref('/categories')
.orderByChild('Recipies' //this is the category)
.endAt(true)
.limitToLast(4)
.on('value', snapshot => {
const arrayOfKeys = Object.keys(snapshot.val())
.sort()
.reverse();
const results = arrayOfKeys
.map((key) => snapshot.val()[key]);
const createLastKey = arrayOfKeys[arrayOfKeys.length - 1];
//just passing the initial data with redux here... (snapshot and lastKey...)
});
};
} else {
//subsequent fetch if there is a lastKey to reference start point
return () => {
firebase.database().ref('/categories')
.orderByChild('Recipies' //this is the category)
.endAt(true, '-LY9OSc7u8wTNQaJ7BXL' //this is the lastKey)
.limitToLast(3)
.on('value', snapshot => {
const arrayOfKeys = Object.keys(snapshot.val())
.sort()
.reverse()
.slice(1);
const results = arrayOfKeys
.map((key) => snapshot.val()[key]);
const createLastKey = arrayOfKeys[arrayOfKeys.length - 1];
const concatImages = _.concat(currentImages, results);
//passing the new data with redux here... (snapshot and lasy ley...)
}
});
};
};
所有这些问题消失时,我只需将查询与startAt().limitToFirst()
改用orderByChild()
。
真的很感谢所有帮助我这个问题搞定了,干杯!
回答如下:因此,尝试几乎所有的东西后,事实证明一切我所要做的就是添加startAt(true)
了。我不知道为什么,但它的工作原理。我有一个是几乎与此相同,如果没有它的工作的其他查询 - 击败我......很想知道为什么我需要这个,以便它工作,虽然。
这里是我工作的代码:
const fetchCategoryImages = (category, currentImages, lastKey) => {
if (!lastKey) {
return () => {
firebase.database().ref('/categories')
.orderByChild('Recipies' //this is the category)
//THE SOLUTION
.startAt(true)
.endAt(true)
.limitToLast(4)
.on('value', snapshot => {
const arrayOfKeys = Object.keys(snapshot.val())
.sort()
.reverse();
const results = arrayOfKeys
.map((key) => snapshot.val()[key]);
const createLastKey = arrayOfKeys[arrayOfKeys.length - 1];
//just passing the initial data with redux here... (snapshot and lastKey...)
});
};
} else {
//subsequent fetch if there is a lastKey to reference start point
return () => {
firebase.database().ref('/categories')
.orderByChild('Recipies' //this is the category)
//THE SOLUTION
.startAt(true)
.endAt(true, '-LY9OSc7u8wTNQaJ7BXL' //this is the lastKey)
.limitToLast(3)
.on('value', snapshot => {
const arrayOfKeys = Object.keys(snapshot.val())
.sort()
.reverse()
.slice(1);
const results = arrayOfKeys
.map((key) => snapshot.val()[key]);
const createLastKey = arrayOfKeys[arrayOfKeys.length - 1];
const concatImages = _.concat(currentImages, results);
//passing the new data with redux here... (snapshot and lasy ley...)
}
});
};
};