0

我有一個簡單的Firestore數據庫,只有少數國家和每個國家/地區的城市。考慮每個國家的文件,即法國。同時使用請求變量讀取文檔面臨PERMISSION_DENIED異常以下兩種情況。Cloud Firestore - 請求變量未按預期工作

,供大家參考分享以下幾點 1)公司的FireStore配置 2),這是不工作 3)相關日誌每次失敗 3)類文件創建和讀取公司的FireStore數據庫 4)方法調用下面的具體配置場景

方案1 - 2之後應許可工作中的至少一種,但兩者都沒有工作

allow read : if request.resource.__name__ != null ; 
allow read : if request.resource.__name__ == null ; 

錯誤日誌

I/System.out的:firestoreTest ACTION_READ onFailureListener E = com.google.firebase.firestore.FirebaseFirestoreException:PERMISSION_DENIED:缺少或權限不足。 I/System.out的:firestoreTest getOnCompleteListener不成功


方案2 - 儘管文件名 '法國' 在數據庫中存在,它不工作。

allow read : if request.resource.__name__ == /databases/$(database)/documents/Countries/france ; 

錯誤日誌

I/System.out的:FireStoreTest.firestoreTest動作= [2],國= [法國],hashmapCountry = [零] I/System.out的:firestoreTest ACTION_READ onFailureListener e = com.google.firebase.firestore.FirebaseFirestoreException:PERMISSION_DENIED:權限缺失或不足。 I/System.out的:firestoreTest getOnCompleteListener不成功


方案3 - 沒有問題,而且運作良好。只有提出,因爲類似的情景2不工作

allow read : if resource.__name__ == /databases/$(database)/documents/Countries/france ; 

錯誤日誌

I/System.out的:FireStoreTest.firestoreTest行動= [2],國= [法國],hashmapCountry = [空] I/System。出:firestoreTest getOnCompleteListener成功{城市=巴黎}


基本配置測試場景

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

     allow write : if true ;  
     allow read : // replace as per below scenario 

      }  
    }  
} 

}


方法來創建集合,僅需要被稱爲試驗前一次

HashMap<String, String > hashMap = new HashMap<>() ; 
hashMap.put("city","paris") ; 
fireStoreTest.firestoreTest(fireStoreTest.ACTION_ADD,"france",hashMap); 

方法可用於測試所有以下3場景

fireStoreTest.firestoreTest(fireStoreTest.ACTION_READ,"france",null); 

下面類是用來測試上述3個場景

package com.firestoreTest ; 

import com.google.firebase.firestore.DocumentReference; 
import com.google.firebase.firestore.DocumentSnapshot; 
import com.google.firebase.firestore.FirebaseFirestore; 

import java.util.HashMap; 
import java.util.Map; 


public class FireStoreTest { 

public static final int ACTION_ADD = 1 ; 
public static final int ACTION_READ = 2 ; 
private FirebaseFirestore firebaseFirestore ; 

public FireStoreTest() { 
    firebaseFirestore = FirebaseFirestore.getInstance() ; 
} 

public void firestoreTest(int action, String country, HashMap hashmapCountry) 

{ 
    System.out.println("FireStoreTest.firestoreTest action = [" + action + "], country = [" + country + "], hashmapCountry = [" + hashmapCountry + "]"); 

    DocumentReference documentReference = firebaseFirestore.collection("Countries").document(country) ; 

    switch (action) 
    { 
     case ACTION_ADD: 



      documentReference.set(hashmapCountry) 
        .addOnFailureListener(e-> System.out.println("firestoreTest ACTION_ADD onFailureListener e = "+e) ) 
        .addOnCompleteListener(task -> { 

         if (task.isSuccessful()) 
         { 
          System.out.println("firestoreTest setOnCompleteListener Successful "); 
         } else 
         { 
          System.out.println("firestoreTest setOnCompleteListener Unsuccessful " +task.getException()); 
         } 

        }) ; 





      break ; 

     case ACTION_READ: 

      documentReference.get() 
        .addOnFailureListener(e-> System.out.println("firestoreTest ACTION_READ onFailureListener e = "+e)) 
        .addOnCompleteListener(taskDocumentSnapshot->{ 

         if (taskDocumentSnapshot.isSuccessful()) 
         { 
          DocumentSnapshot documentSnapshot = taskDocumentSnapshot.getResult() ; 

          if (documentSnapshot.exists()) 
          { 
           Map mapDocumentSnapshot = documentSnapshot.getData() ; 

           System.out.println("firestoreTest getOnCompleteListener Successful "+mapDocumentSnapshot); 

          } else 
          { 
           System.out.println("firestoreTest getOnCompleteListener no data found "); 
          } 

         } else 
         { 

          System.out.println("firestoreTest getOnCompleteListener Unsuccessful "); 
         } 

        }) ; 
      break ; 
    } 

} 

}所有

回答

0

收到的響應來自Firebase支持

問題出在使用request.resource。如our documentation所述,request.resource(及其所有變量,如request.resource。名稱和request.resource.data.city)用於寫入操作,但不用於讀取操作。