错误域= FIRFirestoreErrorDomain代码= 7“缺少权限或权限不足。”

时间:2020-10-02 23:19:54

标签: ios firebase google-cloud-firestore firebase-security

所以我有以下代码从firestore中检索文档,该函数在我的viewDidLoad()中调用:

func loadAnnotations() {
    
    db.collection("jumpSpotAnnotations").getDocuments { (querySnapshot, error) in
        if let error = error {
            print("There was an issue retrieving data from Firestore: \(error)")
            
            let alertController = UIAlertController(title: "Oops!", message: "We are having trouble loading the cliff jumping locations.  Be sure to check your internet connection.", preferredStyle: .alert)
            
            let okayAction = UIAlertAction(title: "Okay", style: .cancel, handler: nil)
            
            alertController.addAction(okayAction)
            
            self.present(alertController, animated: true, completion: nil)
        } else {
            if let snapshotDocuments = querySnapshot?.documents {
                //print(snapshotDocuments)
                for doc in snapshotDocuments {

其余功能并不重要。检索数据时出现错误:Error Domain = FIRFirestoreErrorDomain Code = 7“缺少权限或权限不足。”当我第一次得到这个信息时,我意识到这是由于自从应用程序的Firebase项目创建的第一天以来,我还没有更新我的安全规则,并且已经过去了30天,因此Google将无法再访问直到我更新规则。这些是原始规则:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write;
    }
  }
}

就此应用而言,现在我可以很好地与用户创建和读取任何数据。但是,我不希望它们能够修改或删除数据。所以我在Stack上问了一个有关如何做到这一点的问题,并在这里得到了回答:How to prevent Firestore documents being deleted / modified, but allow all reads and writes现在我更新的安全规则是:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /collection1/{jumpSpotAnnotations} {
      allow read, create;
    }
  }
}

但是,当我尝试检索数据时,仍然出现完全相同的错误。新的安全规则已经发布。所以我的问题是,在按publish进行正式更新之后,我需要做些什么吗,还是让我的模拟器能够检索数据?还是我的安全规则中的代码或Xcode中的函数不正确的问题?为了我想做的请记住,Xcode中的功能可以与原始规则完美地配合使用(我将规则完全打开时就像刚创建Firestore项目时那样,因此我可以轻松地在模拟器中测试)。任何帮助深表感谢!

1 个答案:

答案 0 :(得分:-1)

在回答上一个问题时,我建议您调出各个馆藏的名称。您只是拿走了我给的东西,就逐字抄了下来。您需要在规则中使用实际集合的名称:

    match /jumpSpotAnnotations/{id} {
      allow read, create;
    }

同样,我强烈建议您阅读我链接的文档,以便您了解安全规则的工作原理。