如何从 Firestore 规则中的请求路径获取数据

Jon*_*han 3 firebase-security google-cloud-firestore

我试图弄清楚如何从 or 上的路径获取数组,update以便create根据路径数据创建验证器:

function test() {

  // option 1
  return string(request.path).split('/')[0] == 'databases';

  // option 2
  // return string(request.resource.__name__).split('/')[0] == 'databases';
}

match /posts/{document} {
  allow update: if test();
  ...
}
Run Code Online (Sandbox Code Playgroud)

我已经尝试了前面的两个示例和request.resource.__name__...request.path如何从请求的路径中解析数据?

谢谢,

J

Pet*_*ina 5

让我们假设这是我的 request.path /databases/%28default%29/documents/posts/123456

function test(docId) {
  // request.path supports map access.
  return request.path[4] == '123456'; // this will return true;
  return request.path[3] == 'posts'; // this will return true;
  return docId == '123456'; // this will return true;
}
match /posts/{documentId} {
  allow update: if test(documentId);
}
Run Code Online (Sandbox Code Playgroud)

因此,如果您的路径有 5 段,request.path[4]&&request.path[3]将返回最后 2 段。