Search code examples
javascriptfirebasefirebase-realtime-databaseionic5

Increment and Decrement Firebase Ionic 5


I would like of increment +1 and decrement -1 in database firebase with the simple method

increment() {
  const i = +1;
  this.service.update(i)
}


decrement() {
  const i = -1;
  this.service.update(i)
}

and service I have the simple code

update(panel: Panel) {
return this.db.object(`${this.path}/${panel.codeLed}`)
    .update(panel);
}

Solution

  • I found this code and to me, worked fine

    incrementUp(panel: Panel) {
       return this.db.object(`${this.path}/${panel.codeLed}/up`)
        .query.ref.transaction(ups => {
          if (ups === null) {
            return ups = 1;
          } else {
            return ups + 1;
          }
        });
    }