8000 - Fixes the transaction logic for stars (need to handle the case if t… · dendisuhubdy/quickstart-js@cf39153 · GitHub
[go: up one dir, main page]

Skip to content

Commit cf39153

Browse files
author
Nicolas Garnier
committed
- Fixes the transaction logic for stars (need to handle the case if the transaction object is null)
- Fixed logic where the incorrect UID was used in ref Change-Id: Iaaf6aac46dceb964017af34d496a8c90c671f33d
1 parent 238141a commit cf39153

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

database/scripts/main.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,17 @@ function writeNewPost(uid, username, title, body) {
6262
// [START post_stars_transaction]
6363
function toggleStar(postRef, uid) {
6464
postRef.transaction(function(post) {
65-
if (post.stars && post.stars[uid]) {
66-
post.starCount--;
67-
post.stars[uid] = null;
68-
} else {
69-
post.starCount++;
70-
if (!post.stars) {
71-
post.stars = {};
65+
if (post) {
66+
if (post.stars && post.stars[uid]) {
67+
post.starCount--;
68+
post.stars[uid] = null;
69+
} else {
70+
post.starCount++;
71+
if (!post.stars) {
72+
post.stars = {};
73+
}
74+
post.stars[uid] = true;
7275
}
73-
post.stars[uid] = true;
7476
}
7577
return post;
7678
});
@@ -80,7 +82,7 @@ function toggleStar(postRef, uid) {
8082
/**
8183
* Creates a post element.
8284
*/
83-
function createPostElement(postId, title, text, author) {
85+
function createPostElement(postId, title, text, author, authorId) {
8486
var uid = firebase.auth().currentUser.uid;
8587

8688
var html =
@@ -167,7 +169,7 @@ function createPostElement(postId, title, text, author) {
167169
// Bind starring action.
168170
var onStarClicked = function() {
169171
var globalPostRef = firebase.database().ref('/posts/' + postId);
170-
var userPostRef = firebase.database().ref('/user-posts/' + uid + '/' + postId);
172+
var userPostRef = firebase.database().ref('/user-posts/' + authorId + '/' + postId);
171173
toggleStar(globalPostRef, uid);
172174
toggleStar(userPostRef, uid);
173175
};
@@ -256,7 +258,7 @@ function startDatabaseQueries() {
256258
postsRef.on('child_added', function(data) {
257259
var containerElement = sectionElement.getElementsByClassName('posts-container')[0];
258260
containerElement.insertBefore(
259-
createPostElement(data.key, data.val().title, data.val().body, data.val().author),
261+
createPostElement(data.key, data.val().title, data.val().body, data.val().author, data.val().uid),
260262
containerElement.firstChild);
261263
});
262264
};

0 commit comments

Comments
 (0)
0