当前位置: 首页 > 知识库问答 >
问题:

Firestore的Firebase安全规则引发错误。where()快照

郭阳泽
2023-03-14

获取错误:

OnSnapshot中未捕获的错误:FirebaseError:权限缺失或不足。

下面是正在调用的函数:

let unsubscribeUserThings
export function getUserThings(user, state) {
    unsubscribeUserThings = firebaseDB.collection("things").where("user", "==", user)
        .onSnapshot(function(querySnapshot) {
            var things = {}
            querySnapshot.forEach(function(doc) {
                things[doc.id] = doc.data()
            })

            state.setState({
                things: things
            })
        })

}
export function stopListeningToUserThings() {
    // Stop listening to changes
    unsubscribeUserThings()
}

Firebase安全规则如下:

service cloud.firestore {
  match /databases/{database}/documents {

    // FUNCTIONS

      // True if the signed in user is the thing's user
      function isThingUser() {
        return request.auth != null && request.auth.uid != null && resource.data.user == request.auth.uid
      }

      // True if the signed in user is the thing's owner
      function isThingOwner() {
        return request.auth != null && request.auth.uid != null && resource.data.owner == request.auth.uid
      }

    // RULES

      // Things
      match /things/{thingID} {
        allow create: if true
        allow read, update: if isThingUser() || isThingOwner()
      }
  }
}

奇怪的是,它仍然正确地显示一切,只是触发了这个错误。为什么会这样?我忽略了吗?

共有1个答案

武卓
2023-03-14

Firebase规则一切都很好--它最终被一个不同的onSnapshot调用拉出来,以监听“thing”文档的子集合,子集合“parts”。添加了一个嵌套的match语句,以授予它读取/things/{thingID}/parts/{partID}的权限,现在一切都在工作!

service cloud.firestore {
  match /databases/{database}/documents {

    // FUNCTIONS

      // True if the signed in user is the thing's user
      function isThingUser() {
        return request.auth != null && request.auth.uid != null && resource.data.user == request.auth.uid
      }

      // True if the signed in user is the thing's owner
      function isThingOwner() {
        return request.auth != null && request.auth.uid != null && resource.data.owner == request.auth.uid
      }

  // RULES

    // Things
    match /things/{thingID} {
      allow create: if true
      allow read, update: if isThingUser() || isThingOwner()

      match /parts/{partID} {
        allow read, write: if true
      }
    }
  }
}
 类似资料:
  • 在Firestore中,我有一个拥有所有者的项目集合。 所有者可以是用户,应该允许用户读取其文档。 使用可以执行以下操作: 但是对于引用字段,在涉及安全规则时似乎没有任何文档。 我尝试了以下安全规则: 并如下所示进行查询: 但我仍然缺少或权限不足。

  • 最近我收到了一系列Firebase通知,涉及: [Firebase]您的Cloud FiRecovery数据库有不安全的规则 我们检测到您的安全规则存在以下问题:任何用户都可以写入您的整个数据库。因为您的项目没有强大的安全规则,任何人都可以访问您的整个数据库。攻击者可以窃取、修改或删除您的数据,并会抬高您的账单` Edit2:我需要的是允许每个人都写,而不需要登录,但是只有管理员帐户应该能够从Fi

  • 我最近收到一封来自Firebase的电子邮件,告诉我我的数据库(FiRest)的规则不安全,所以我将它们更改为以下内容: 在我有这些规则之前: 在做出改变后,电子邮件不断地回来,我不知道还能做什么。我已经尝试了以下链接中给出的几个选项,但它们都不能满足我的需要。 https://firebase.google.com/docs/rules/insecure-rules#firestore 我需要授

  • 简单介绍一下,我正在尝试了解Firebase安全协议,我已经建立了一个名为UsersDB的数据库,该数据库将根据身份验证存储详细信息。uid。详细信息包括全名、电子邮件、提供商、帐户创建日期、上次登录日期。 我设置了如下规则: 我的理解是,记录只能由user_id符合auth.uid.的人读写 我的问题是我做得对吗?如果不对,我应该如何实现这一目标?我只希望创建帐户的人能够读写这个,而没有其他ui

  • 我试图为我的云还原设置安全规则,但我对规则应该应用于哪个数据库感到困惑。 我目前使用的唯一功能是记录Firebase事件。安全规则是否适用于Firebase事件?如果是,应该为哪个数据库编写规则?如果没有,我是否可以拒绝对所有事件的读/写访问,并且事件仍将被记录并发送到BigQuery?

  • 我有两个收藏-租赁和用户。 租赁单据有一个名为“LandlorDid”的字段,类型为引用(而不是字符串)。 现在,在我的Firestore安全规则中,我希望只有当租用的landlordID字段与发出请求的用户的uid(即匹配时,才允许更新租用。 这应该很简单,但get()根本不起作用。Firebase文档,滚动到“Access other Documents”(访问其他文档)对我的情况没有任何帮助