[
I am trying to query the post-collection with the user settings but the settings is an array of more than 10 elements and nothing is returned. I know the documents did mention the limit of 10 elements, does anyone know a workaround?
firebaseApp.collection('posts')
.where("newTag", "in", mySettings)
.get()
let array = [];
posts.forEach((post) => {
array.push(post.data());
});
dispatch({ type: ActionTypes.GET_POSTS, payload: array });
A simple function to chunk the array could solve your problem:
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 };
Then a for await hack to get the snaps would work as well:
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);
}
The workaround is to perform a query for each item in mySettings
individually, and merge the results on the client. Or, split mySettings
into another collection of arrays that each have 10 or less items, query for each one of those individually, and merge the results on the client.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With