0
votes

Having some issues with reading data. I can write just fine according to my security rules. See below for my query in swiftui code and my security rules. For context, I have a users collection and a routines collection. Each routine document has a uid that is tied to a user. Anyone know why I might not be able to read correctly here (which is affecting my ability to then subsequently write?

Security Rules

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /users/{userId} {
      allow read, write: if request.auth != null && request.auth.uid == userId;
    }
    match /routines/{routine} {
        allow write: if request.auth != null && request.auth.uid == request.resource.data.uid;
      allow read: if request.auth != null && resource.data.uid == resource.data.uid;
    }
  }
}

Swift Query Code

func updateRoutine() {
    db.collection("routines").whereField("name", isEqualTo: "temp routine").getDocuments() { (querySnapshot, err) in
        if let err = err {
            print("Error getting documents: \(err)")
        } else {
            for document in querySnapshot!.documents {
                self.db.collection("users").document(self.currUser?.uid ?? "").updateData(["routinePreference": document.documentID])
                self.db.collection("routines").document(document.documentID).updateData(["name": "another temp routine"])
                return
            }
        }
        
    }
}

EDIT This is the error I'm getting:

Error getting documents: Error Domain=FIRFirestoreErrorDomain Code=7 "Missing or insufficient permissions." UserInfo={NSLocalizedDescription=Missing or insufficient permissions.} 2020-07-03 01:43:24.440221-0400 TestRulesApp[58965:7804974] 6.26.0 - [Firebase/Firestore][I-FST000001] Listen for query at routines failed: Missing or insufficient permissions.

1
What is the error you're getting from Firestore? - Jeremy
Edited to show the error i'm getting - glothais-kwl

1 Answers

1
votes

You are updating data here.The technique to use is incoming-field-value-equal-existing-field-value. So you should have your update rule allow update:if request.resource.data.uid == resource.data.uid;