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

629
Visualizações
How to get all documents in Firestore (v9) sub collection

I'm trying to get all documents in all sub-collection with Firebase V9, so I used collectionGroup to retrieve the data.

Here is my Firestore architecture :

bots (collection)
    | id (doc ID)
        | order_history (sub collection)
            | id (doc ID)
                createdBy: uid (user uid)

enter image description here

And this is how I try to get documents :

const [orders, setOrders] = useState([]);
const { user } = useAuth();

const getOrders = async () => {
    const orders = await getDocs(query(collectionGroup(db, 'order_history'), where('createdBy', '==', user.uid)));
setOrders(orders.docs.map((doc) => ({
  ...doc.data()
})))
  }

useEffect(() => {
    getOrders();
    console.log('orders ', orders); // return []
}, []);

This code returns an empty array.

Did I do something wrong?

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

0

Your getOrders anonymous method execution requires an explicit return statement if there's more than 1 statement. Implicit returns work when only a single statement exists (and after some testing, return await X doesn't appear to work either).

const getOrders = async () => {
    const orders = await getDocs(...);
    setOrders(orders.docs.map(...))
}

Needs to be


const getOrders = async () => {
    const orders = await getDocs(...);
    return setOrders(orders.docs.map(...))
}
about 4 years ago · Juan Pablo Isaza Relatório

0

I think your getOrders function is asynchronus function. If you want debug log I think you should waiting for getOrders completed then orders had been updated.

Ex:

useEffect(() => {
    console.log('orders ', orders);
}, [orders]);
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