Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

262
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!