Angularfire2 the best way to increase the value?

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.

+5
source share
5 answers

There is no need to define an object or 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 with data in a data location and therefore prevent conflicts; for example, two users are updating the same value at the same time.

You can also use the template literal in your path if you want. :) (Pay attention to the checkmarks 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 .

+10
source

"angularfire2 V5" Solution:

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

Firestore now has increment() , which correctly increases the field, even if several users are competing for editing at the same time

Increasing atomically with Cloud Firestore

To use this in angularfire2

 import { firestore } from 'firebase'; incrementLike(userToIncrementLike) { const increment = firestore.FieldValue.increment(1); const userLike = this.af.doc('users/${userToIncrementLike.uid}'); userLike.update({ likes: increment }); } 

I used Firestore directly since I could not find FieldValue in angularfire2.

+1
source

@ corner / fire (corner version 7)

https://firebase.google.com/docs/firestore/manage-data/transactions https://firebase.google.com/docs/firestore/query-data/get-data

 incrementLike(userToIncrementLike) { //VARIABLE FOR MANAGING THE SCOPE var that = this; this.afs.firestore.collection("users").where("id", "==", userToIncrementLike.uid) .get() .then(function(querySnapshot) { querySnapshot.forEach(function(doc) { // doc.data() is never undefined for query doc snapshots console.log(doc.id, " => ", doc.data()); return that.afs.firestore.runTransaction(function(transaction) { // This code may get re-run multiple times if there are conflicts. return transaction.get(doc.ref).then(function(doc) { if (!doc.exists) { throw "Document does not exist!"; } //THIS IS WHERE TO DO THE INCREMENT var new_score = doc.data().score + 1; transaction.update(doc.ref, { score: new_score }); }); }).then(function() { console.log("Transaction successfully committed!"); }).catch(function(error) { console.log("Transaction failed: ", error); }); }); }) .catch(function(error) { console.log("Error getting documents: ", error); }); } 
0
source

I use this on Firestore. The same code is used almost for working with the database in real time.

 "@angular/fire": "^5.1.2" const login = this.afs.doc('/users/${uid}'); login.valueChanges() .pipe(take(1)) .subscribe((user: User) => { login.update({ login: user.login as number + 1 }); }); 
0
source

Source: https://habr.com/ru/post/1265023/


All Articles