Firestore 数据库规则缺失或权限不足

Missing or insufficient permissions firestore database rules

我正在自学 firestore,我想不出一种只允许用户更新、删除或只读取他们添加的集合的方法。

这是我正在使用的结构:

我使用 firebase 身份验证进行用户处理。我在每个集合的数据库中将 currentUser.uid 保存为 user_id

这些是我正在使用的规则

service cloud.firestore {
  match /databases/{database}/documents {

    match /tasks{
      allow read, update, delete: if request.auth.uid == resource.data.user_id;
      allow create: if request.auth.uid != null;
    }
  }

当我尝试 read/get 数据时,我得到 Missing or insufficient permissions 错误。

我正在为 firestore 使用网络 api (JavaScript)。这是我用来读取数据的代码。

function read() {

    db.collection("tasks").get().then((querySnapshot) => {
        querySnapshot.forEach((doc) => {
            var newLI = document.createElement('li');

            newLI.appendChild(document.createTextNode(doc.data().task));

            dataList.appendChild(newLI);

        });
    });

}

这个其实在Firestore Documentation上面有解释(推荐阅读)

您在 /tasks 之后缺少一个通配符:

service cloud.firestore {
  match /databases/{database}/documents {
    match /tasks/{task} {
      allow read, update, delete: if request.auth.uid == resource.data.user_id;
      allow create: if request.auth.uid != null;
    }
  }
}

错误出在我的 JavaScript 我得到了所有内容而没有被用户过滤

function read() {
    let taskColletion = db.collection("tasks");

    taskColletion.where("user_id", "==", firebase.auth().currentUser.uid).get().then((querySnapshot) => {
        querySnapshot.forEach((doc) => {
            var newLI = document.createElement('li');

            newLI.appendChild(document.createTextNode(doc.data().task));

            dataList.appendChild(newLI);
        });
        
    });

}