簡體   English   中英

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;
    }
  }

當我嘗試讀取/獲取數據時,出現Missing or insufficient permissions錯誤。

我正在為 firestore 使用 web 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);

        });
    });

}

錯誤出在我的 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);
        });
        
    });

}

這實際上在Firestore 文檔中有解釋(我建議閱讀它)。

您在/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;
    }
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM