我尝试使用用户设置查询后集合,但设置是一个包含 10 多个元素的数组,并且没有返回任何内容。我知道文档确实提到了 10 个元素的限制,有人知道解决方法吗?
firebaseApp.collection('posts')
.where("newTag", "in", mySettings)
.get()
let array = [];
posts.forEach((post) => {
array.push(post.data());
});
dispatch({ type: ActionTypes.GET_POSTS, payload: array });
最佳答案
一个简单的数组分块函数可以解决您的问题:
const chunkArray = (list: any[], chunk: number): any[][] => {
const result = [];
for (let i = 0; i < list.length; i += chunk) {
result.push(list.slice(i, i + chunk));
}
return result;
};
export { chunkArray };
然后使用 for wait 黑客来获取快照也可以工作:
const snaps_collection: FirebaseFirestore.QuerySnapshot[] = [];
for await (const snap of chunks.map(
async (chunk) =>
await database
.collection("collection_name")
.where("id", "in", chunk)
.get()
)) {
snaps_collection.push(snap);
}
关于javascript - Firebase Firestore查询超过10个元素的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59257753/