使用swift(Firebase数据库)更新Post中的点赞数


var postRef:DatabaseReference {
return Database.database().reference().child("posts")
}
@IBAction func LikePostTapped(_ sender: UIButton) {
postRef.observeSingleEvent(of:.value, with: { [self] (snapshot) in
if snapshot.children.allObjects is [DataSnapshot] {

count =  count + 1
LikeCount.text = "(count)"
LikeCount.textColor = UIColor.red
postRef.updateChildValues(["likes":count])
}
})

}

我正在尝试更新我数据库中每个帖子的点赞数量。然而,当我点击点赞按钮时,它会将点赞数量保存在firebase中的新子节点中,而不是特定帖子上的点赞子节点中。这是一张图片,说明了我的意思。

图片链接:https://i.stack.imgur.com/64wLn.png

使用特定的postId(posts-child(来获得与我使用roomId一样保存点赞的确切位置使用updateChildValues更新以前的类似状态:

func updateStatus (roomId: String, totalLikes: String) {
self.dbReference.child("posts").child(roomId).updateChildValues(["likes": totalLikes])
}

最新更新