@@ -62,15 +62,17 @@ function writeNewPost(uid, username, title, body) {
62
62
// [START post_stars_transaction]
63
63
function toggleStar ( postRef , uid ) {
64
64
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 ;
72
75
}
73
- post . stars [ uid ] = true ;
74
76
}
75
77
return post ;
76
78
} ) ;
@@ -80,7 +82,7 @@ function toggleStar(postRef, uid) {
80
82
/**
81
83
* Creates a post element.
82
84
*/
83
- function createPostElement ( postId , title , text , author ) {
85
+ function createPostElement ( postId , title , text , author , authorId ) {
84
86
var uid = firebase . auth ( ) . currentUser . uid ;
85
87
86
88
var html =
@@ -167,7 +169,7 @@ function createPostElement(postId, title, text, author) {
167
169
// Bind starring action.
168
170
var onStarClicked = function ( ) {
169
171
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 ) ;
171
173
toggleStar ( globalPostRef , uid ) ;
172
174
toggleStar ( userPostRef , uid ) ;
173
175
} ;
@@ -256,7 +258,7 @@ function startDatabaseQueries() {
256
258
postsRef . on ( 'child_added' , function ( data ) {
257
259
var containerElement = sectionElement . getElementsByClassName ( 'posts-container' ) [ 0 ] ;
258
260
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 ) ,
260
262
containerElement . firstChild ) ;
261
263
} ) ;
262
264
} ;
0 commit comments