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

271
Visualizações
Uncaught (in promise) TypeError: querySnapshot.forEach is not a function React Native

I have a 'users' collection on Firebase. This collection has some fields in it which I'd like to render on the screen

I have a class Home which contains the following function:

const db = firebase.firestore();

    
export class Home extends Component {
  constructor(props) {
    super()
  }
  componentDidMount(){
     db.collection('/users')
     .doc(firebase.auth().currentUser.uid)
     .get()
     .then(querySnapshot => {
        querySnapshot.forEach(uid => {
        let data = uid.data();
           console.log(data);
        })
      })
  }
}

Without the .doc(firebase.auth().currentUser.uid), I get the all the fields of all the users on the screen but when I add this, to get the details per user, I encounter the error "Uncaught (in promise) TypeError: querySnapshot.forEach is not a function". Thanks in advance for the help.

enter image description here

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

0

You are using get() on a DocumentReference which returns a DocumentSnapshot that contains data of a single document only and there isn't any forEach method present on it. So just using data() directly on the snapshot should do. Try refactoring the code as shown below:

componentDidMount() {
  db.collection('/users')
    .doc(firebase.auth().currentUser.uid)
    .get()
    .then((docSnapshot) => {
      console.log(docSnapshot.data())
    })
}

If you are trying to get all the documents from users collection, then use get() on CollectionReference (essentially remove the .doc(uid)) as shown below:

componentDidMount() {
  db.collection('/users')
    .get()
    .then((querySnapshot) => {
      querySnapshot.forEach((doc) => {
        console.log(doc.data())
      })
    })
}
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