【问题标题】:Firebase realtime database, Check if firebase database value exists or not?Firebase 实时数据库,检查 firebase 数据库值是否存在?
【发布时间】:2019-02-15 12:51:25
【问题描述】:

我正在使用 Firebase 实时数据库,但我需要一些帮助来解决这个问题。
这是我的代码:

  let usersDB = Database.database().reference().child("user/\(username)")
        usersDB.observeSingleEvent(of: .value, with: { (snapshot) in
            if snapshot.exists() {
                self.lblUserAvailability.text = "Not Available"
            }
            else {
               self.lblUserAvailability.text = "Available"
            }
        })
    }

我想要的是,如果用户已经存在于数据库中,它应该转到“snapshot.exists()”语句,但在我的情况下,它总是转到 else 语句。 我的数据库结构是:

用户

-LXa4s3d9BrGqDThdWU-

用户名“rashid55”

【问题讨论】:

  • 删除此child("user/\(username)") 并添加此child("user")
  • 但是我想检查一个特定的值我的意思是一个特定的用户名请检查这个图片以获得更好的想法i.stack.imgur.com/8eCSK.png
  • @Rashid Latif 在下面检查我的答案,它会起作用
  • 请让我检查一下

标签: ios swift firebase firebase-realtime-database


【解决方案1】:

您需要监听“用户”节点并查询您正在搜索的名称

let usersDB = Database.database().reference().child("user").queryOrderedByChild("username").queryEqualToValue(username)
    usersDB.observeSingleEvent(of: .value, with: { (snapshot) in
        if snapshot.exists() {
            self.lblUserAvailability.text = "Not Available"
        }
        else {
           self.lblUserAvailability.text = "Available"
        }
    })
}

【讨论】:

  • 非常感谢@Shahzaib Qureshi
猜你喜欢
  • 2021-01-14
  • 2016-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-14
  • 1970-01-01
  • 2016-09-21
相关资源
最近更新 更多