0

I have a problem with firebase queries. I have post documents (image) and I want to query for all posts that have multiple selected strains in their "strains" array (lets say in the case of the image all posts that have the strains "test" and "anothertest" in there). Is there a way to query for that and get around the error I am getting?

"fireFuncs.js:158 Uncaught (in promise) FirebaseError: A maximum of 1 'ARRAY_CONTAINS' filter is allowed per disjunction."

these are exerpts of the functions i use for the query

} else if (selectedStrains.length > 0) {
      if (findPostMode == "or") {
        queryCollectionLimited(
          "posts",
          "strains",
          "array-contains-any",
          selectedStrains.map((s) => s.name),
          (recPosts) => {
            setPosts(recPosts);
          },
          20
        );
      } else {
        multiQueryCollection(
          "posts",
          getAndModeDocs(selectedStrains),
          (result) => {
            setPosts(result);
          }
        );
      }
}

the getAndModeDocs func :

function getAndModeDocs(selectedStrains) {
    const queries = [];
    forArrayLength(selectedStrains, (strain) => {
    const strainArrayQuery = ["strains", "array-contains", strain.name];
    queries.push(strainArrayQuery);
    });
    return queries;
}

the multiQueryCollection function:

async function multiQueryCollection(col, queries, onResult) {
  let qList = [];
  forArrayLength(queries, (query) => {
    qList.push(where(query[0], query[1], query[2]));
  });
  const colRef = collection(db, col);
  let q = query(colRef, ...qList);
  const snapshot = await getDocs(q);
  let docs = [];
  snapshot.forEach((d) => {
    docs.push(d.data());
  });
  if (onResult) onResult(docs);
  return docs;
}

Firestore Image

dnAir
  • 197
  • 1
  • 9

0 Answers0