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

Firestore安全规则错误:FirebaseError:权限缺失或不足

司寇经亘
2023-03-14

意向行为

目标是修改我的安全规则,以便只有经过身份验证的用户才能读写该特定用户的数据。这是基于users UID,这是我们如何识别单个用户的(这是通过firebase身份验证完成的,所以不确定问题是什么)。

我的数据库结构

service cloud.firestore {
  match /users/{userId} {
   allow read: if request.auth.uid == userId;
  }
}

FirebaseError:权限缺失或不足

任何关于我在这里能做什么的建议都将不胜感激。我在其他问题中找不到任何适用的东西。

我的查询

fbDB.collection('users').doc(user.uid).collection('practitionerClients').doc(trackingClientNameCheck).collection("sessions").doc(trackingClientNameCheck + currentTime).set({

    name: trackingClientName,
    timeOfSession: currentTime,
    vocValues: vocValueSaved,
    sudsValues: sudsValueSaved, 
    recallValues: recallValueSaved,
    moodValues: moodValueSaved,
    clientNotes: trackingClientNotes,
    triggeringEventProcessed: trackingTriggeringEvent,
    positiveBelief: trackingPositiveBelief,
    negativeBelief: trackingNegativeBelief

}).then(function() {
    console.log("Document successfully written!");

})

.catch(function(error) {
    console.error("Error writing document: ", error);
});;

*我的错误

Error getting document: FirebaseError: Missing or insufficient permissions.

    at new t (https://www.gstatic.com/firebasejs/6.2.4/firebase-firestore.js:1:47216)
    
    at t.fromRpcStatus (https://www.gstatic.com/firebasejs/6.2.4/firebase-firestore.js:1:236502)
    at t.fromWatchChange (https://www.gstatic.com/firebasejs/6.2.4/firebase-firestore.js:1:245756)
    at t.onMessage (https://www.gstatic.com/firebasejs/6.2.4/firebase-firestore.js:1:189313)
    at https://www.gstatic.com/firebasejs/6.2.4/firebase-firestore.js:1:188114
    at https://www.gstatic.com/firebasejs/6.2.4/firebase-firestore.js:1:188899
    at https://www.gstatic.com/firebasejs/6.2.4/firebase-firestore.js:1:60566

共有1个答案

翟曦之
2023-03-14

您需要在规则中指定该子集合

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

    match /users/{userId} {
      allow read: if request.auth.uid == userId;

      match /practitionerClients/{clientId} {
        allow read: if request.auth.uid == userId;
      }

    }
  }
}

无论何时添加类似的新子集合,通常都必须指定它或为该路径添加通配符规则。所以你可以这样做,而不是上面的规则

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

    match /users/{userId}/{document=**} {
      allow read: if request.auth.uid == userId;
    }
  }
}

你可以在这里阅读更多

 类似资料:
  • 我的Firestore数据库有一些用户和一些s。如果没有其他人认领,用户可以认领。他们通过在上创建一个文档来声明,该文档有一个名为的字段,该字段是对他们自己的用户文档的引用。 我想我已经在这些安全规则中捕捉到了: 但是,当为所有权删除文档时,我获得了: 我是这样做的: 如果我打开read和write设置为true的文档,就不会出现此异常。 我的规则怎么了?他们用模拟器测试正常。

  • 我使用的是Role(允许读,写:如果request.auth.uid!=null),当我登录时,我得到的数据是ok的,但当我注销用户时,我得到的错误是:缺少或不充分的权限。首先,我认为这是因为我没有取消订阅我尝试的可观察的(rxjs/operator/takewhile)即使我使用异步管道,我也得到了同样的错误。

  • 我在自学firestore,我想不出一种方法,只允许用户更新、删除或阅读他们添加的集合。 我使用firebase auth进行用户处理。对于每个集合,我将作为保存在数据库中。 这是我的规矩 当我试图读取/获取数据时,我得到错误。 我正在使用firestore的web api(JavaScript)。这是我用来读取数据的代码。

  • 我编写了一些与默认规则完美配合的代码。代码如下: 但如果我用这些规则 我明白了 我做错了什么?

  • 使用以下安全规则: 我的应用程序似乎使用进行了正确的身份验证,因为返回正确的用户ID。 我是不是做错了什么?