首页 > 解决方案 > 更新 Firestore 上的规则后,我无法读取数据(React Native)

问题描述

我设置的规则是允许读取: 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

标签: javascriptfirebasereact-nativegoogle-cloud-firestore

解决方案


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

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

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

推荐阅读