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

361
Visualizações
Firebase TypeError: querySnapshot.forEach is not a function in React

I'm integrating firebase with React for the first time and I am just following the documentation, this is the code given in the firebase cloud firestore getStarted section:

enter image description here

but when I have tried to copy the same code it gives me an error of TypeError: querySnapshot.forEach is not a function

enter image description here

I'm basically trying to get the data from a collection, document.

here is the code that I have copied:

import React, { useState, useEffect, useRef } from "react";
import { db } from "./firebase.config.js";
import * as firestore from "firebase/firestore";
export default function App() {
    const addEntry = () => {
        try {
            const docRef = firestore.addDoc(firestore.collection(db, "users"), {
                first: "Alan",
                middle: "Mathison",
                last: "Turing",
                born: 1912,
            });
            console.log("Document written with ID: ", docRef.id);
        } catch (e) {
            console.error("Error adding document: ", e);
        }
    };
    const querySnapshot = firestore.getDocs(firestore.collection(db, "users"));
    console.log(querySnapshot);
    querySnapshot.forEach((doc) => {
        console.log(`${doc.id} => ${doc.data()}`);
    });

return (
    <div>
        <button onClick={addEntry}>add</button>
    </div>
   );
}

How can I solv this error

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

0

The getDocs() returns a promise and might not have resolved when you are trying to read the docs. Try refactoring the code as shown below:

firestore.getDocs(firestore.collection(db, "users")).then((querySnapshot) => {
    querySnapshot.forEach((doc) => {
        console.log(`${doc.id} => ${doc.data()}`);
    });
});

Also you can just import getDocs() instead of using namespaces like V8 SDK:

import { getDocs } from "firebase/firestore";
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