swift - Firebase 评论未出现在数据库中

标签 swift firebase firebase-realtime-database firebase-authentication

我是 Firebase 的新手。我正在使用 swift 。我想将用户的评论存储在我的数据库中,但 Firebase 上似乎什么也没有显示。我的代码有问题吗? 感谢您的帮助。

@IBAction func OnSubmitTouched(_ sender: UIButton) {
    if (textField.text != ""){

       let ref = Database.database().reference()
       let commentsReference = ref.child("comments")
       let newCommentID = commentsReference.childByAutoId().key
       let newCommentReference = commentsReference.child(newCommentID)
        guard let currentUser = Auth.auth().currentUser else {
            return
        }
        let currentUserID = currentUser.uid
        newCommentReference.setValue(["uid": currentUserID, "commentText": textField.text!]) { (error, ref) in
            if error != nil {
               // ProgressHUD.showError(error!.localizedDescription)
                return
            }
        }

最佳答案

听起来您希望用户能够发表评论。用户可以选择登录,在这种情况下您还想存储他们的 UID。

执行此操作的非常简单的方法(我也不是 Swift 专家)是:

    var currentUserID = "anonymous"
    if let currentUser = Auth.auth().currentUser {
        currentUserID = currentUser.uid
    }
    newCommentReference.setValue(["uid": currentUserID, "commentText": textField.text!]) { (error, ref) in
        if error != nil {
            print(error!.localizedDescription)
            return
        }
    }

现在 uid属性将具有发表评论的用户的 UID,或者 "anonymous"如果用户未登录。

关于swift - Firebase 评论未出现在数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52008521/

相关文章:

带有 AnyView 的 SwiftUI 对象数组

firebase - 在 firebase 动画列表中,有没有办法让 firebase 列表在加载之前知道小部件的预期高度?

ios - 在 childByAutoId Firebase Swift 中访问特定数据

firebase - Cloud Functions for Firebase - 获取当前用户 ID

ios - FIRDatabaseHandle 值或 childChanged

android - 永远不会调用 Firebase populateViewHolder

reactjs - MISSING_CUSTOM_TOKEN

Swift canOpenURL() 错误 iOS9 隐私问题电话 ://and telprompt://

ios - Xcode 在 Storyboard 中隐藏项目

ios - 在 iOS 中显示 View 而不是默认通知