【问题标题】:Swift Firebase queryingSwift Firebase 查询
【发布时间】:2018-03-05 07:36:04
【问题描述】:

我正在尝试根据他的电子邮件获取 uid。

我有这个:

let usersRef = Database.database().reference().child("users")

    usersRef.queryOrdered(byChild: "email").queryStarting(atValue: email).queryEnding(atValue: email).observeSingleEvent(of: .value, with: { (snapshot) in
        if let values = snapshot.value as? [String: Any]{
            print(values)
        }
    })

我的数据库如下所示:

用户

| -$uid

| --email: "一些电子邮件"

快照不返回任何内容。

此外,调试器告诉我:

使用未指定的索引。您的数据将在客户端下载和过滤。考虑将 /users 处的 ".indexOn": "email" 添加到您的安全规则中以获得更好的性能

我想我是这样做的:

{
  "rules": {
"users": {
  ".read": "auth != null",
  "$uid": {
    ".write": "$uid === auth.uid",
    ".indexOn": ["email"]
  }

但这也不好用。

【问题讨论】:

  • 预期输出是什么?请解释查询的目的是什么?
  • 问题已更新。
  • 试试这个:usersRef.queryOrdered(byChild: "email").queryEqual(toValue:email).observeSingleEvent
  • 如果没有看到确切的 JSON,就很难确定发生了什么。您可以通过单击 Firebase Database console 中的“导出 JSON”链接并将其添加到您的问题中来获得此信息吗?
  • 谢谢,3stud1ant3,成功了。

标签: swift firebase firebase-realtime-database


【解决方案1】:

您必须在执行查询的位置定义 Firebase 数据库索引。所以这比你目前拥有的水平高一级:

{
  "rules": {
    "users": {
      ".indexOn": ["email"],
      ".read": "auth != null",
      "$uid": {
        ".write": "$uid === auth.uid",
      }
    }
  }
}

【讨论】:

    【解决方案2】:

    如下更改:

    {
        "rules": {
        "users": {
            ".read": "auth != null",
            "$uid": {
            ".write": "$uid === auth.uid",
            ".indexOn": "email"
        }
        }
    }    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-22
      • 2016-10-14
      • 2018-04-01
      • 2017-05-06
      • 1970-01-01
      • 2016-06-25
      • 2017-03-25
      • 2019-07-28
      相关资源
      最近更新 更多