【问题标题】:How are likes recorded in firebase after a like button is pressed?按下赞按钮后,如何在firebase中记录赞?
【发布时间】:2019-07-19 14:46:45
【问题描述】:

我一直致力于在 firebase 中记录喜欢的内容。 (“喜欢的人的UID”进入“他喜欢的人的孩子参考”)。

如果按下操作,我已经连接了插座,并且有代码可以将其放入 firebase,但在某处我正在为可选选项生成 nil。

    @IBAction func likePressed(_ sender: Any) {
    self.like.isEnabled = false
    let ref = Database.database().reference()
    let keyToPost = ref.child("likes").childByAutoId().key

    ref.child("people").child(self.postID).observeSingleEvent(of: .value, with:  {(snapshot) in

        if let people = snapshot.value as? [String: AnyObject] {
            let updateLikes: [String: Any] = ["peopleWhoLike/\(keyToPost)" : Auth.auth().currentUser!.uid]
            ref.child("people").child(self.postID).updateChildValues(updateLikes, withCompletionBlock: { (error, rerr) in

                if error == nil {
                       ref.child("people").child(self.postID).observeSingleEvent(of: .value, with: { (snap) in
                        if let properties = snap.value as?[String: AnyObject]{
                            if let likes = properties["peopleWhoLike"] as? [String : AnyObject] {
                                let count = likes.count
                                self.likeLabel.text = "\(count) Likes"

                            let update = ["likes" : count]
                                ref.child("people").child(self.postID).updateChildValues(update)


                            }

                        }
                    })
                }
            })

        }

    })

    ref.removeAllObservers()



}

//////////////inside homepage override func Table View:

 cell.postID = self.people[indexPath.row].postID

这是Json数据库结构:

"people" : {
"9RYUttVhCzXKs6H1XnZ63TZ8Dun2" : {
  "Coordinates" : {
    "latitude" : 15.78583,
    "longitude" : -4.406417
  },
  "Education" : "CS",
  "PhotoPosts" : "https://firebasestorage.googleapis.com/v0/b/daylike-2f938.appspot.com/o/images%2FPhotoPosts?alt=media&token=ac9e574f-b55f-4bf0-b75b-79b877a86480",
  "caption" : 1563439869418,
  "users" : "nane19@aol.com"
},
"K1eqsVZfKGgIf0sS1UZcPNxY62x1" : {

在 firebase 数据库的最后,在用户 Ronald 下,喜欢他的人将被列出(更准确地说是他们的 uid)。这将在新的子 postID 下。

【问题讨论】:

    标签: swift firebase firebase-realtime-database


    【解决方案1】:

    所以我发现了错误。数据库没有子postID,应该和uid一样。在登录时添加该孩子后,错误已修复。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-26
      • 1970-01-01
      • 2012-11-01
      • 1970-01-01
      • 2020-04-29
      • 1970-01-01
      • 1970-01-01
      • 2021-09-18
      相关资源
      最近更新 更多