swift - 如何在数据库 Firebase 中搜索

标签 swift firebase firebase-realtime-database

我在 Stackoverflow 上进行了搜索,但没有找到我正在寻找的解决方案

我想在数据库 Firebase 中搜索,如果“名称”等于导航标题中的名称,并且用户用于登录的“电子邮件”等于数据库中的电子邮件,那么它将避免用户添加新项目

Json 看起来像这样

"Items" : {
      "-KUMSKLFqMjclbqygnPL" : {
      "Item" : "asdfg"
      "Name" : "Fadi”,
      "User Email" : "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="177e577e3974787a" rel="noreferrer noopener nofollow">[email protected]</a>"
      }
    }

我的代码是

    let databaseRef = FIRDatabase.database().reference()

    var NAME : String!
    var UserEmail : String!
    var email : String!

    databaseRef.child("Items").queryOrderedByKey().observe(.childAdded, with:  { snapshot in

        let snapshotValue = snapshot.value as? NSDictionary
        NAME = (snapshotValue?["Name"] as? String)!

         UserEmail = (snapshotValue?["User Email"] as? String)!
      email = (FIRAuth.auth()?.currentUser?.email)!

       if NAME == navigationItem.title {

            if UserEmail == email {
                print ("you can't add new item \(UserEmail)")
            }else if UserEmail != email {
                print("you can add new Item")

            }
       }
    })

我的问题是如果名称等于 Fadi 但有两个不同的电子邮件 结果将是

you can't add new item Optional("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8fe6cfe6a1ece0e2" rel="noreferrer noopener nofollow">[email protected]</a>")
you can add new Item

它应该只打印

you can't add new item Optional("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="acc5ecc582cfc3c1" rel="noreferrer noopener nofollow">[email protected]</a>")

我不知道如何打破它,现在看起来它循环遍历所有数据库

更新:

我尝试德拉威答案

打印结果(snapShot.value):

    "-KUMS7J-rCglHrVGX840" =     {
    "Item" = "asdsd"            
    "Name" = "a";
    "User Email" = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="325372531c515d5f" rel="noreferrer noopener nofollow">[email protected]</a>";
    };

结果是对的!但现在我需要从该节点提取用户电子邮件

最佳答案

如果您正在寻找具有特定名称的节点,请尝试以下操作:-

  let databaseRef = FIRDatabase.database().reference()
  databaseRef.child("Items").queryOrdered(byChild: "Name").queryEqual(toValue: "Fadi").observeSingleEvent(of: .value, with: { (snapShot) in

        if let snapDict = snapShot.value as? [String:AnyObject]{

              for each in snapDict{
                      let key  = each.key as! String
                      let name = each.value["Name"] as! String
                      print(key)
                      print(name)
                 }
            }
        }, withCancel: {(Err) in

            print(Err.localizedDescription)

    })

关于swift - 如何在数据库 Firebase 中搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40194937/

相关文章:

android - Firebase Android : What is the difference between getting callback from DatabaseReference's setValue via Task and DatabaseReference. CompletionListener?

ios - 单击 UIImageView SWIFT 不工作

ios - 防止 UITextView.typingattributes 在 Swift 中的换行符 (\n) 上应用 backgroundColor

Firebase Analytics - 无法检索 Firebase 实例 ID

android - 当应用程序处于后台时,在 Firebase 服务中编辑 SharedPreferences 后,值将会更改

Firestore 的 Firebase 分析器

ios - Firebase快照中是否存在唯一的用户名

java - 查询后 Firebase 获取子值

ios - 连接到 firebase/firestore 时出错 : Could not reach Cloud Firestore backend

ios - UITabBar 屏幕顶部