Getting invalid_argument error in Firestore Database Rules

Viewed 83

I have set the following rule in my Firestore Database rules.

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
  
    function getUser(userId) {
        let user = get(/databases/$(database)/documents/users/$(userId));
      return user;
    }
    
    function getRole(userId) {
        let roleId = getUser(userId).data.role;
        let role = get(/databases/$(database)/documents/roles/$(roleId)).data; // ERROR
      return role;
    }
  
    function isPermitted(entity_action, userId) {
        return getRole(userId)[entity_action];
    }
  
    match /action_items/{action_item_id} {
      allow get, list, read: if request.auth != null && isPermitted("action_item.read", request.auth.uid);
    }
  }
}

But get the following error on simulation.

Error running simulation — Error: simulator.rules line [12], column [17]. Function not found error: Name: [get].;

Error: Invalid argument provided to call. Function: [get], Argument: ["||invalid_argument||"]

Any idea how I can resolve this error that I'm getting? Attaching a screenshot of the console. Firebase Console Error Screenshot

enter image description here

1 Answers

The problem seems to be value of role in user document. It's value roles/admin which is not a valid document ID. That essentially like fetching a document with roles/roles/admin that looks a bit like path to a collection.

Try changing the value of role in user's document to just admin and also document ID in roles collection should be admin.

Users collection:

enter image description here

Roles collection:

enter image description here

Related