TypeError: Cannot read property 'toString' of undefined

Hi guys,

I’m having problem with subscribe function. When I delete something from my firebase database and it’s throwing me an error of TypeError: Cannot read property ‘toString’ of undefined. Any ideas why? Seems like its trying toString non existing value.

This is the way I pull all the information:

    this.afDatabse.list('/events').snapshotChanges().subscribe(data => {
      this.events2 = data;   
      this.events2.forEach((event) => {
        //console.log(event.key)
        this.afDatabse.list(`/events/${event.key}`).valueChanges().subscribe(data1 => {       
          this.events3.push({
            key: event.key,
            start: new Date(data1[3].toString()),
            end: new Date(data1[2].toString()),
            title: data1[4],
            color: colors.red,
            allDay: data1[0]
          });

          this.events$ = this.events3;
          console.log(this.events$);
          this.refresh.next();
         
        })
        //this.events$ = this.events3;
        //console.log(this.events3);
      });
     
      this.events3 = [];
    })

The way I delete it ( but it brakes even if I’m doing it on firebase):

  deleteEvent(key: string, event: CalendarEvent): void {
   
    this.afDatabse.list(`/events/${key}`).remove();
    
    console.log('deleted' + key);
    
  }

Hello,
At that time you call whatever.toString() whatever does not exist. check if date[3] and data[2] are exsiting at the time you call it. Make investigation why does it not exist.

Best regards,

Hi anna, thank you. I found a solution. The problem that its not a good idea to subscribe in subscribe… I accessed the values by looking into snapshotChanges() :wink: event.payload.node_.children_.root_.value.value_