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

查看:18
本文介绍了无法让 Firestore 规则 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||"]

推荐答案

问题出在您定义函数的范围内.由于您将 isProjectOpenForAssign 定义在与此匹配 match/projects/{anyProject} 相同的级别,因此该函数将无法访问 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的匹配中定义函数.

    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 规则 get() 在函数内工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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