Use a transaction to update a value on two different nodes

I have two different nodes in the database.

  • all posts

  • users

According to the branching model, when a user adds a message, it is updated as all posts

well as on users/uid/posts

.

Each post consists of a button that displays the number of likes.

When the user clicks on it, the value should increase by +1.

According to the docs we use transaction

for this kind of processes.

But the problem with using transaction

is that it only updates one node as far as I know

But my problem is how should I update this transaction in both nodes as above.

Use method update

How can you use a transaction that is updated on both nodes

+3


source to share


1 answer


You can use all your logic to update your database server side with Cloud Functions for Firebase . A database trigger can be used to respond to the data being written to the database and then execute some JavaScript to make sure the fan ends properly. This will have the advantage of ensuring that all changes take place independently of the client.



Transactions cannot modify data in two different locations at the same time, but you probably want to use it in your client and in the cloud functions to make sure that the simultaneous writing won't have a problem.

+2


source







All Articles