Search code examples
angularfirebaseionic2angular2-servicesangularfire2

angularfire2 best way to increment a value?


I searched in many forums, questions, in doc but can't find the correct solution.

Problem

What is the best way to increment a value using angularfire2 ?
I saw we could use [transaction()][] but it's not for angularfire2 actually. Or with snapshot ?

user.service.ts

incrementLike(userToIncrementLike){
    this.af.database.object('users/' + userToIncrementLike.uid).subscribe((userObject) => {
      var newUser = {
        likes: userObject.likes + 1
        };

     });
     this.af.database.object('users/' + userToIncrementLike.uid).update(newUser);

  }

I tried also in this way :

incrementLike(userToIncrementLike){

    let obs = this.af.database.object('users/' + userToIncrementLike.uid);
    obs.subscribe((snapshot) => {
      let newValue = (snapshot.$value) ? (snapshot.$value + 1) : 1;
      obs.set(newValue);
    });
  }

Thank you very much for your help and tips :) Luis.


Solution

  • It isn't necessary to define an object or to use the update() method. The object already exists in the database, so you can just work on it there. This is actually the purpose of transaction()--to work on the data at the data location and therefore prevent conflicts; e.g., two users updating the same value at the same time.

    You can also use the template literal in your path if you like. :) (Note the backticks instead of single quotes.)

    incrementLike(userToIncrementLike){
        this.af.database.object(`users/${userToIncrementLike.uid}/likes`).query
        .ref.transaction(likes => {
            if (likes === null) {
                return likes = 1;
            } else {
                return likes + 1;
            }
        })
    }
    

    Update: September, 2019. Use query instead of $ref.