Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

162
Views
Error when querying for specific field inside an array inside a document in Firebase V9

I posted a question earlier about how to query for a specific value inside an array. I was told that there was a similar question already asked. I have looked at the solution for that question however i am getting an error. This is my code currently to check to see if the field mal_id inside the favourites array contains an id passed by me.

const docRef = collection(db, 'users')
const q = query(docRef, where('favourites', 'array-contains', {mal_id: id}))

I then attached an onSnapshot method to see if the data exists which is like this

onSnapshot(q, (snapshot) => {
    if(snapshot.data()){
      console.log(true)
    }
    else{
      console.log(false)
    }
  })

But i get an error saying snapshot.data is not a function. idk if you are not supposed to use an onSnapshot method on this kind of query or my code is wrong. I have attached a picture of my database structure. Thanks.

*Edit So, i have changed my data structure. Now i am creating a new field called animeID and then storing the id into it. I then do the array contains onto that but the snapshot still gives me the same error. Here is the updated code along with a new picture of my database structure.

const docRef = collection(db, 'users')
  const q = query(docRef, where('animeID', 'array-contains', `mal_id: ${id}`))
  onSnapshot(q, (snapshot) => {
    if(snapshot.data()){
      console.log(true)
    }
    else{
      console.log(false)
    }
  })

Updated firestore structure

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

As Doug commented, Firestore's array-contains operator checks for equivalence between the value you supply and an entire array item. It can't find a subset of an item.

The common workaround is to add an additional field to your document with just the mal_id values you want query on. So something like:

Favourites_mal_ids: [21]

And then you can perform an array-contains on that field.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!