Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

164
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda