Firestore query with 'array-contains-any' operator not returning expected data

42 Views Asked by At

Update - @Frank van Puffelen Here are the screenshots on the data I am expecting from firestore from my webpage -

Screenshot 1

This is the data I am expecting from .where('qS', 'array-contains-any', [1,2]) enter image description here

ScreenShot 2 This is the data I am expecting from .where('qS', 'array-contains-any', [2]) enter image description here

If I was not clear before - The operand value getFV changes based on button clicks from three buttons, setting return to [1,2], [1], or [2]. I created a method to manage this dynamic change, avoiding the need for multiple conditional statements for each button click scenario. FV from console - fv from console

Even though the getFV method returns the exact array needed for the operand in this where clause .where('qS', 'array-contains-any', fV) (Attached sccreenshot tagged - FV from console), it consistently retrieves data from Screenshot 1, regardless of the buttons clicked.

However, when I manually input the same array that was returned from the Screenshot 2 directly into the code, like this .where('qS', 'array-contains-any', 1), I get the desired data. fv from console single

I am using Firestore to fetch data based on a filter that utilizes the 'array-contains-any' operator. Here is the code I am using to fetch the data -

const fV = this.getFV(this.selectedButton);
return qnaCollectionRef
.where('qS', 'array-contains-any' , fV)
.onSnapshot((querySnapshot) => {

here is the code for getFv

getFV(qF: QF): number[] {
    debugger;
    switch (qF) {
      case QF.All:
        return [1,2];
      case QF.O:
        return [1];
      case QF.A:
        return [2];
      default:
        throw new Error('Unsupported filter option');
    }
  }

Assume that my firestore db contains data like

for QF.All - [abc, dce, efg, hij]
for QF.O - [abc, dce]
for QF.A - [efg, hij]

Now I have a button which changes this fV to [1,2], 1, 2 which gets called in below query

.where('qS', 'array-contains-any' , fV)

The issue I'm facing is that regardless of the filter values I pass in fVs, the query always returns data from the fv [1,2]. However, when I modify the query and hardcode the operand to 1 or 2, it correctly returns the desired data from 1 or 2.

.where('qS', 'array-contains-any', [2])
0

There are 0 best solutions below