Property 'toJSON' does not exist on type 'DatabaseSnapshot<any>'

I was testing the code supplied in this website: Ionic 4 Firebase CRUD and ran into a problem with the "Delete Item from List Data" section:

bookingRes.snapshotChanges().subscribe(res => {
  this.Bookings = [];
  res.forEach(item => {
    let a = item.payload.toJSON();
    a['$key'] = item.key;
    this.Bookings.push(a as Appointment);
  })
})

I get an error saying "Property 'toJSON' does not exist on type 'DatabaseSnapshot'. Property 'toJSON' does not exist on type 'DatabaseSnapshotExists'.ts(2339)".

As I understand everything is up to date

Upvotes: 0

Views: 5163

Answers (3)

Carey Turner
Carey Turner

Reputation: 21

You can try this. This is the only answer I was able to find to avoid using toJSON which no longer exists? let a = JSON.parse(JSON.stringify(item.payload));

Upvotes: 2

It seems that I was missing dependencies and reinstalled Firebase and angular/fire as mentioned in this thread: Github Firebase

Upvotes: 0

Natixco
Natixco

Reputation: 661

Try with one of these:

let a = JSON.parse(item.payload);

or

let a = item['payload'].toJSON();

Upvotes: 0

Related Questions