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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…