3

I have the following document:

"players" : [ 
  {
    "id" : userid,
    "name" : "dissident"
  }, 
  {
    "teamId" : teamId,
    "name" : "ink"
  }
]

This is what I tried, but it is not working

this.afs.collection('matches', ref => ref.where('players', 'array-contains', {teamId: teamId}))
13
  • can you please elaborate more on it what is your expected output? Commented Jan 8, 2020 at 6:45
  • What's the error? Commented Jan 8, 2020 at 6:49
  • @Mridul Im trying to get this document only if the players array contains the given team id Commented Jan 8, 2020 at 6:50
  • Are you referring JSON as document? and you want to return the whole object if teamid matches with given team id? Commented Jan 8, 2020 at 6:54
  • 2
    You may be interested by this answer: stackoverflow.com/questions/54081799/… which explains that it is not possible, with array-contains, to query for a specific property of an object stored in an array Commented Jan 8, 2020 at 8:53

2 Answers 2

1

Your error is this:

FirebaseError: Function Query.where() called with invalid data. Unsupported field value: undefined (found in field teamId)

This means you passed an invalid value of undefined for teamId in the query, which is invalid. You should debug your code and figure out why teamId is undefined, then fix that.

Sign up to request clarification or add additional context in comments.

2 Comments

I even tried hardcoding the id to get the data it is not working
That sounds like a different problem than the one you first posted. Maybe ask another question if it's different by nature?
1

To solve this, please change the following line of code:

this.afs.collection('matches', ref => ref.where('players', 'array-contains', {teamId: teamId}))

to

this.afs.collection('matches', ref => ref.where('players', 'array-contains', {teamId: teamId, name : 'ink'}))

As you can see, this is actually possible only if you query for the entire object. But be aware, as Doug Stevenson mentioned in his answer if you pass an invalid value of undefined for teamId or for name, you'll always get the error that you are talking about.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.