我正在尝试首先加载用户的最新帖子,并以降序排列它们,但在将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()
时,所有这些问题都消失了。
非常感谢我在此问题上能获得的所有帮助,干杯!
答案 0 :(得分:1)
因此,在尝试了几乎所有内容之后,事实证明,我要做的就是也添加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...)
}
});
};
};
答案 1 :(得分:0)
我只是用此JSON进行测试:
{
"-LY8EYaWHINB1khsIEvJ" : {
"Recipies" : true,
"username" : "User1"
},
"-LY8Em4B6COFk3how5FC" : {
"Buds" : true,
"username" : "User2"
},
"-LY8Eq2E1muFcOBstODa" : {
"Buds" : true,
"username" : "User3"
},
"-LY8Esi98QdhszIgvRRN" : {
"Buds" : true,
"username" : "User4"
},
"-LY8EymPGxK8Y_YnRfC0" : {
"Buds" : true,
"username" : "User5"
},
"-LY8F0RrYbLNPpYwIuMX" : {
"Buds" : true,
"username" : "User6"
},
"-LY8F3QfERAhOq3iW3jC" : {
"Receipies" : true,
"username" : "User7"
},
"-LY9OSc7u8wTNQaJ7BXL" : {
"Buds" : true,
"username" : "User8"
},
"-LYDaIrnDKIWndMcLE-g" : {
"Recipies" : true,
"username" : "User8"
}
}
这段代码:
ref.orderByChild('Buds')
.endAt(true, '-LY9OSc7u8wTNQaJ7BXL')
.limitToLast(3)
.once('value', snapshot => {
snapshot.forEach(child => {
console.log(child.key+": "+JSON.stringify(child.val()));
})
})
实时示例:https://jsbin.com/zigofok/edit?js,console
运行时打印:
“-LY8EymPGxK8Y_YnRfC0:{\” Buds \“:true,\” username \“:\” User5 \“}”
“-LY8F0RrYbLNPpYwIuMX:{\” Buds \“:true,\” username \“:\” User6 \“}”
“-LY9OSc7u8wTNQaJ7BXL:{\” Buds \“:true,\” username \“:\” User8 \“}”
请注意,由于该用户没有User7
属性,因此如何跳过Buds
。