I searched in many forums, questions, in the doc , but cannot find the right solution.
Problem
What is the best way to increment a value with angularfire2?
I saw that we could use [transaction ()] [], but this is not for angularfire2. Or with a 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 also tried as follows:
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 advice :) Louis.
source share