i'm trying to setup this event booking webapp from a source code that I downloaded. When i first set it up it throws me this error:
db.js:3 Uncaught TypeError: Cannot read properties of undefined (reading 'doc')
at Object.next (db.js:3:47)
at next (database.ts:1973:20)
at async_observer.ts:51:11
I searched the error to find a solution for this issue, but I couldn't find any. I'd greatly appreciate it if someone could help me with this issue. I have provided my code below. Thanks!
db.collection("events").onSnapshot((snapshot) => {
// Handle the latest event
const newestEvent = snapshot.docChanges()[0].doc.data();
const id = snapshot.docChanges()[0].doc.id;
showLatestEvent(newestEvent, id);
// shift the latest event element
snapshot.docChanges().shift();
snapshot.docChanges().forEach((event) => {
showEvents(event.doc.data(), event.doc.id);
});
});
CodePudding user response:
Most likely snapshot.docChanges()
is an empty array, so snapshot.docChanges()[0].doc.data()
then fails. You'll want to check for an empty result set before accessing a member by its index like that.