简体   繁体   中英

How can I fix my firestore database setup error?

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);
  });
});

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM