亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Firestore 沒有執行操作的權限

Firestore 沒有執行操作的權限

九州編程 2022-06-09 10:24:40
我正在嘗試在 Firestore 中設置規則,如果每個人都通過了應用程序的身份驗證,每個人都可以讀取彼此的內容,但只有文檔的所有者才能創建、寫入、更新或刪除它們。我在 Firestore 中設置了以下規則:rules_version = '2';service cloud.firestore {  match /databases/{database} {    match /codes/{userID} {      allow create, write, update, delete: if request.auth.uid == userID;      allow read: if request.auth.uid != null;    }  }}我的 Firestore 結構是:Collection 'codes' > Document 'BNhBibYZ0ThCNCH2gzPRufFsIk22' > nothing in here我的要求是:firestore()  .collection('codes')  .doc(this.state.userID)  .onSnapshot((querySnapshot) => {    if (!querySnapshot) {      let testObj = {"hello": "world"}       firestore().collection('codes').doc(this.state.userID).set(testObj);    }  });我收到的輸出是:Error: [firestore/permission-denied] The caller does not have permission to execute the specified operation.我究竟做錯了什么?
查看完整描述

1 回答

?
揚帆大魚

TA貢獻1799條經驗 獲得超9個贊

您可以使用我創建的以下函數來執行此操作


function isUserAuthenticated() {

    return request.auth.uid != null; 

}


function belongsTo(userId) {

    return request.auth.uid == userId;

}


然后你可以像這樣使用它:


rules_version = '2';

service cloud.firestore {

  match /databases/{database}/documents {

    match /{document=**} {

      allow read, write: if false;

    }


    match /codes/{userId} {

      allow read : if isUserAuthenticated();

      allow create : if belongsTo(userId);

      allow update: if belongsTo(userId);

      allow delete: if belongsTo(userId);

    }


    /* Functions */

    function isUserAuthenticated() {

      return request.auth.uid != null; 

    }


    function belongsTo(userId) {

      return request.auth.uid == userId;

    }

  }

}   

如果它適合您,請接受作為答案??稍谠u論中了解更多問題。


查看完整回答
反對 回復 2022-06-09
  • 1 回答
  • 0 關注
  • 96 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號