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

181
Visualizações
Query documents in firestore from an array of ID's

I was wondering how I can query documents from a firestore collection from an array of ID's? I only want the documents in the collection that are in the array of ID's. I looked at another answer and think my approach is correct, however, I am getting an error.

(node:15105) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'data' of undefined
>      at /Users/username/SideProjects/projectname/functions/index.js:40:38
>      at processTicksAndRejections (internal/process/task_queues.js:95:5)

The error happens because the function is not finding any documents in the collection from that array of ID's. However, I double-checked the database and know that there are documents in the collection with ID's from the array.

const admin = require('firebase-admin')

....

let feedItems = db.collection(feedItemsCollection)

feedItemsList = feedItems.where(admin.firestore.FieldPath.documentId(), 'in', ['HPOorsSnbHpTYwwXxfWw']).get().then(snapshot2 => {
       console.log(admin.firestore.FieldPath.documentId())
       console.log("In feed Items")
       //console.log(feedItemIds)
       console.log(snapshot2[0])

       //error happens on this line because snapshot2[0] returns undefined
       console.log(snapshot2[0].data())
})

Snapshot2[0] returns undefined which I'm assuming means that no data was returned. I think I'm not properly calling documentId(), but don't know the fix.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

There "maybe" two problems with your code. Follow both points to make sure things are working

  1. Data inside snapshot2 maybe empty

You'll first have to fix your code to test this theory. You're not accessing data from snapshot2 correctly. To do it right, one way is this:

// `snapshot2` will have a `docs` property that you can leverage
const snapshot2Data = snapshot2.docs.map((doc) => doc.data());
  1. .documentId() may not be doing what it's supposed to (as you said)

To test this theory, check if snapshot2Data is empty. Run :

console.log(snapshot2Data); // what do you get ?

If no, it's not empty and you got data back, then you're all set. Nothing more to do

If yes, it is empty, then run :

console.log(admin.firestore.FieldPath.documentId()); // what do you get ?

Did you get back a string? If no, then we have another problem. You'll need to take a closer look at your firebase-admin setup, as well.

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