Firestore 安全规则 - 权限缺失或不足,但我已编写

时间:2021-04-20 19:21:13

标签: javascript firebase google-cloud-firestore

我正在尝试获取公共集合中的特定文档,但收到错误“权限缺失或不足”。

但是,我觉得我写得很好:

match /posts/{userId}/userPosts/{postId} {
  allow read: if isSignedIn() && request.query.limit <= 10;
  allow write, update, delete: if false;
  
  match /likes/{document=**} {
    allow read: if isSignedIn();
    allow write: if false;
  }
  
  match /comments/{document=**} {
    allow read: if isSignedIn();
    allow write: if false;
  }
}

使用此代码,安全规则可以正常工作:

  const query = firestore
    .collection("posts")
    .doc(userId)
    .collection("userPosts")
    .orderBy("date", "desc")
    .startAfter(startAfter);

  const querySnapshot = await query.limit(limit).get();

但是...有了这个,我收到了错误:

  const myPostRef = firestore
    .collection("posts")
    .doc(userId)
    .collection("userPosts")
    .doc(postId);

  const myPostDoc = await myPostRef.get();

为什么我无法获得特定的文档?

1 个答案:

答案 0 :(得分:0)

也许尝试将您的 allow read 规则分解为查询和单个文档的特定规则:

allow list: if isSignedIn() && request.query.limit <= 10;
allow get: if isSignedIn();