我是 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/