0

Hello i'm implementing the array-contain query but everytime i'm trying i'm Getting blank snapshot. My code is:

getStation = () => {
  const query = firebase
     .firestore()
     .collection('routes')
     .where('station', 'array-contains', 'station_name ');

  query.get().then(snapshot => {
      console.log("snapshot", snapshot);

      snapshot.docs.forEach(doc => {
        alert("eri")
        console.log("eryh", doc.data);
      })
    })
    .catch(err => {
      console.log('Error getting documents', err);
    });

}

I even tried to pass the static values but not able to get the desire value.enter image description here

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Balwinder Singh
  • 125
  • 1
  • 13

3 Answers3

1

In your code there is a problem with typo mistake station must be stations

.where('station', 'array-contains', 'station_name ');

must be

 .where('stations', 'array-contains', 'station_name ');

I hope it will fix your problem.

Update Answer

There is also typo mistake in station_name you just added the space after station_name

.where('station', 'array-contains', 'station_name ');

must be

.where('stations', 'array-contains', 'station_name');
Zeeshan Ansari
  • 9,657
  • 3
  • 26
  • 27
0

You are trying to use array-contains to check for a field inside a map. This is not supported.

The array-contains can only search for a field inside the array and not a field inside a map which is inside that array.

A workaround I have tested is that you add an extra field inside the array that will be a boolean or a string and you can query based on it.

Using a boolean field in the array:

enter image description here

.where('stations', 'array-contains', true);

Using a String field in the array:

enter image description here

.where('stations', 'array-contains', 'station_name exists');
Waelmas
  • 1,894
  • 1
  • 9
  • 19
  • The reason you give for why OP's code doesn't work is correct, but your explanation is a bit off, and your workarounds won't work either. It's actually simpler than your explanation: `array-contains` checks if the precise object/value you specify exists in the array. It can't check on subsets of the array elements, only on complete values. – Frank van Puffelen Dec 06 '19 at 14:48
  • Could you please explain a bit more why you believe the workarounds won't work ? – Waelmas Dec 06 '19 at 15:05
  • Hmm... on second reading it *might* indeed work. I was thrown off by the mixed types that you now have in your array. I'd still recommend against mixing types in this way, as it just makes debugging and maintenance harder. Separating out the precise value you want to filter on is the more idiomatic approach for this use-case. – Frank van Puffelen Dec 06 '19 at 15:12
  • I see what you mean. I'll keep this in mind. – Waelmas Dec 06 '19 at 15:58
0

The array-contains operation checks if the precise object/value you specify exists in the array. It can't check on subsets of the array elements, only on complete values. So in your current structure, you'd have to pass all properties into the array-contains call:

.where('station', 'array-contains', 
  { station_address_name: '...', station_lat_long: ..., status_name: '...' });

This typically is not feasible, in which case you'll want to store the elements that you want to filter on in a (additional) separate field in your documents. In this example, since you want to filter on station names, you'd add a field station_names where you store just the station names. Then you can query with:

.where('station_names', 'array-contains', 'name to search for')

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807