【问题标题】:Firestore Security Rules - Missing or insufficient permission, but I have written themFirestore 安全规则 - 缺少权限或权限不足,但我已编写
【发布时间】:2021-04-20 19:21:13
【问题描述】:

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

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

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

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

【问题讨论】:

  • 当你执行一个简单的.get()时你确定request.query.limit一个值吗?
  • 删除这是问题所在!但是……我需要它。有什么方法可以结合这两个规则?
  • 可能是 OR - 类似于“已登录 AND(非查询 OR 限制
  • 你确定发送正确的postId

标签: javascript firebase google-cloud-firestore


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-05
    • 2020-10-27
    • 2018-07-07
    • 2021-06-29
    • 2018-04-25
    • 2019-02-26
    • 1970-01-01
    • 2018-08-14
    相关资源
    最近更新 更多