Can't get Firestore Rules get() to work inside a function

别来无恙 提交于 2019-11-29 12:54:18

The problem is in the scope of where you define your function. Since you define isProjectOpenForAssign at the same level as this match match /projects/{anyProject}, the function won't have access to anyProject.

There are two solutions:

  1. Pass anyProject as a parameter to isProjectOpenForAssign.

    function isProjectOpenForAssign(anyProject) {
      return get(/databases/$(database)/documents/projects/$(anyProject)).data.canAssignTask == true;
    }
    
    match /projects/{anyProject} {
      allow create: if request.auth != null;
    
      match /tasks/{anyTask} {
        allow create: if request.auth != null && (isProjectOpenForAssign(anyProject));
      }
    }
    
  2. Define the function inside the match that declares anyProject.

    match /projects/{anyProject} {
      function isProjectOpenForAssign() {
        return get(/databases/$(database)/documents/projects/$(anyProject)).data.canAssignTask == true;
      }
    
      allow create: if request.auth != null;
    
      match /tasks/{anyTask} {
        allow create: if request.auth != null && (isProjectOpenForAssign());
      }
    }
    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!