Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit 1ffa298

Browse files
committed
[readme] - Update readme for transactions with more examples
1 parent 17b48f5 commit 1ffa298

1 file changed

Lines changed: 22 additions & 1 deletion

File tree

docs/DATABASE.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,9 @@ same time.
416416
You can look at the [docs](https://firebase.google.com/docs/reference/js/firebase.database.Reference#transaction) for more information.
417417

418418
Note that a return value of `null` will delete the value at this location whereas returning
419-
undefined will abort the transaction. On success a promise is returned containing
419+
undefined will not modify the data at this location. Firebase web aborts the transaction when
420+
given an undefined, but due to technically difficulties we just return transaction success which
421+
results in committed = true. On transaction complete a promise is returned containing
420422
{committed:boolean, snapshot: DataSnapshot} and an error will be returned if the transaciton
421423
failed.
422424

@@ -463,6 +465,25 @@ firebaseWebApi.database().ref(path).transaction(currentValue => {
463465
return ++currentValue; // increment the value
464466
}
465467
})
468+
469+
// Based off Firebase simple blog post. You can also treat the
470+
// data as an object and return an updated version of post
471+
firebaseWebApi.database().ref(path).transaction(function(post) {
472+
if (post) {
473+
console.log("Post Object looks like: " + JSON.stringify(post));
474+
if (post.stars && post.stars[uid]) {
475+
post.starCount--;
476+
post.stars[uid] = null;
477+
} else {
478+
post.starCount++;
479+
if (!post.stars) {
480+
post.stars = {};
481+
}
482+
post.stars[uid] = true;
483+
}
484+
}
485+
return post;
486+
});
466487
```
467488
</details>
468489

0 commit comments

Comments
 (0)