0

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?

1 Answer 1

1

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")
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.