Reputation: 45
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?
Upvotes: 0
Views: 119
Reputation: 50830
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")
}
Upvotes: 1