Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

169
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda