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

261
Views
Firebase undefined error when array is empty

Hi so I have this function that counts how many time a name appears in a list:

fire.firestore()
.collection("eventLikes")
.doc(eventID)
.onSnapshot((docSnapshot) => {
      
      //}
    // if(docSnapshot.data().ActivityLikes.length === 0) {
        const nameCounts = docSnapshot.data().ActivityLikes.reduce((acc, cur) => {
            acc[cur.name] = (acc[cur.name] || 0) + 1;
        return acc;
}, {});

docSnapshot.data() is empty at the start and docSnapshot.data().ActivityLikes.length is sometimes empty so [] in firebase. But in NextJs i keep gettin this error:

How should i go about fixing this error?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If the document doesn't exist then .data() returns undefined. You should if that exists first:

if (docSnapshot.exists) {
  // process data
} else {
  console.log("Document does not exists")
}
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!