无法使Firestore Rules get()在函数内工作 [英] Can't get Firestore Rules get() to work inside a function

查看:61
本文介绍了无法使Firestore Rules get()在函数内工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Firestore无法与函数中的 get 配合使用

Firestore doesn't work well with get inside a function

我有这个规则

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

    function isProjectOpenForAssign() {
      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());
      }
    }
  }
}

在运行模拟器测试时,我得到:

When running the simulator testing it I get:

运行模拟时出错-错误:simulator.rules第[23]行,第[14]列.找不到函数错误:名称:[get].错误:提供了无效的参数来调用.函数:[get],参数:["|| invalid_argument ||"]

Error running simulation — Error: simulator.rules line [23], column [14]. Function not found error: Name: [get].; Error: Invalid argument provided to call. Function: [get], Argument: ["||invalid_argument||"]

推荐答案

问题出在定义函数的范围之内.由于您在与此匹配match /projects/{anyProject}相同的级别上定义了isProjectOpenForAssign,因此该函数将无法访问anyProject.

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.

有两种解决方案:

  1. anyProject作为参数传递给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));
  }
}

  • 在声明anyProject的匹配项内定义函数.

  • 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());
      }
    }
    

  • 这篇关于无法使Firestore Rules get()在函数内工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆