【问题标题】:I can`t read the data after I update my rules on firestore (React Native)更新 Firestore 上的规则后,我无法读取数据(React Native)
【发布时间】:2021-03-08 22:29:02
【问题描述】:

我设置的规则是允许读取:if resource.data.uid == request.auth.uid;

我怎么称呼它是这样的

return async (dispatch) => {
    await firebase
      .firestore()
      .collection("patients")
      .doc(firebase.auth().currentUser.uid)
      .collection("patient")
      .orderBy("creation", "desc")
      .get()
      .then((result) => {
        let Patients = result.docs.map((doc) => {
          const data = doc.data();
          const id = doc.id;
          return { id, ...data };
        });
        dispatch({ type: GET_PATIENTS, Patients });
      });
  };

我该如何解决?

firebase 规则

 match /databases/{database}/documents {
    match /patients/{patientsID}{
        match /patient/{patientID}{
        allow read: if resource.data.uid == request.auth.uid;
        allow create: if request.resource.data.uid == request.auth.uid;
        allow update: if request.resource.data.uid == request.auth.uid;
        allow delete: if resource.data.uid == request.auth.uid;
        match /diagnostic/{diagnosticID}{
          allow read: if true;
          allow write: if request.resource.data.uid == request.auth.uid;
        }
      }
    }
  }
}

数据图像

https://1drv.ms/u/s!Ag7uBMx2FuEijuILje2xJIv8RawcFA?e=Gy6jeC

【问题讨论】:

  • 我试图在下面提供帮助。如果不是这样,您能否编辑您的问题(下面有一个链接)以包括:1)重现此问题的最小、完整规则(因为检查可能出现在许多地方),2)截图您希望通过此查询返回的文档。
  • 我明天试试,现在已经晚了,谢谢你的帮助
  • 没有用,我会用你的要求添加问题
  • 在我将索引添加到 Firebase 后,它现在可以工作了,谢谢

标签: javascript firebase react-native google-cloud-firestore


【解决方案1】:

规则不会自行过滤数据。相反,他们只是确保所请求的数据符合您的规则。

所以您的查询需要匹配规则,这意味着它们还需要按 UID 过滤:

  .collection("patient")
  .orderBy("creation", "desc")
  .where("uid", "==", firebase.auth().currentUser.uid)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-01
    • 2019-03-27
    • 1970-01-01
    • 1970-01-01
    • 2020-10-08
    • 2020-12-30
    • 1970-01-01
    相关资源
    最近更新 更多