我正在尝试首先加载用户的最新帖子,并以降序排列它们,但在将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...)
}
});
};
};