13flmd
13flmd

Reputation: 35

Firestore Query multiple child values gets FirebaseError: Invalid Query. A non-empty array is required for 'in' filters

I'm trying to query a collection and see if one the the child values (zipOne, zipTwo, zipThree) is equal to a zip code. Hoping someone could help. I keep on getting an error stating:

FirebaseError: Invalid Query. A non-empty array is required for 'in' filters.

Here are the children I am trying to query and check if my zip code matches any of them in 'servicing'.

FireStore DB

  import { getFirestore,collection,getDocs,doc,query,where,getDoc,setDoc,} from "firebase/firestore";

  const currentServicingZip = "33302";
  const q = query(
    collection(db, "business"),
    where("servicing.zipOne", "in", currentServicingZip),
    where("servicing.zipTwo", "in", currentServicingZip),
    where("servicing.zipThree", "in", currentServicingZip),
    );

  const querySnapshot = await getDocs(q);
  querySnapshot.forEach((doc) => {
    // doc.data() is never undefined for query doc snapshots
    console.log(doc.id, " => ", doc.data());
  });

Upvotes: 0

Views: 2002

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599591

As the error message says, an in filter expects that you pass it an array of values. Even if you have only a single value, it has to be in an array. So:

collection(db, "business"),
  where("servicing.zipOne", "in", [currentServicingZip]),
  where("servicing.zipTwo", "in", [currentServicingZip]),
  where("servicing.zipThree", "in", [currentServicingZip]),

Two things to pay attention to here. First off, this is exactly the same as doing equality checks on those fields:

collection(db, "business"),
  where("servicing.zipOne", "==", currentServicingZip),
  where("servicing.zipTwo", "==", currentServicingZip),
  where("servicing.zipThree", "==", currentServicingZip),

Secondly, the above performs a logical AND query, so only returns documents where zipOne, zipTwo and zipThree all have the same value as currentServicingZip. Firestore currently doesn't support performing an OR condition across multiple fields in a document.

Upvotes: 1

Related Questions