Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript firestore安全规则资源。数据为空对象_Javascript_Google Cloud Firestore_Firebase Security - Fatal编程技术网

Javascript firestore安全规则资源。数据为空对象

Javascript firestore安全规则资源。数据为空对象,javascript,google-cloud-firestore,firebase-security,Javascript,Google Cloud Firestore,Firebase Security,在firestore安全规则中,resource.data始终是一个emtpy对象,这是一个bug还是什么 我的firestore规则: service cloud.firestore { match /databases/{database}/documents { match /hospitals/{document=**}{ // allow read :if resource.data.size() == 0; //this return true, resou

在firestore安全规则中,resource.data始终是一个emtpy对象,这是一个bug还是什么

我的firestore规则:

service cloud.firestore {
  match /databases/{database}/documents {
    match /hospitals/{document=**}{

      // allow read :if resource.data.size() == 0; //this return true, resource.data is an empty object

          allow read :if resource.data.name != null; // this doesn't work
    }
  }
}
我的javascript:

auth().onAuthStateChanged((user) => { 
  if (user) {

    //db is the firestore instance
    db.collection('/hospitals').get()
      .then(printResult)

  } else {
    
  }
}) 
这是我当前的数据库快照

解决了的: 谢谢弗兰克的回答

在我的例子中,问题在于当我们查询多个文档时,firestore安全性不会评估实际的文档值

//this doesn't firestore doesnt' evaluate the documetn
db.collection('hospitals').get()

//this will work ,if you need to compare the actual value
db.document('hospitals/somehospital').get()

安全规则本身不过滤数据。它们只是对客户端可以读取的数据实施规则。您的客户目前正在尝试读取所有医院的信息。由于您的安全规则对客户端可以读取的数据有限制,因此它们拒绝此操作

您需要通过与安全规则匹配的查询读取数据,以确保客户端请求的内容不超过安全规则允许的内容。大概是

db.collection('/hospitals')
  .where("name", ">=", "")
  .get()
  .then(printResult)
请注意,这确实要求文档具有
名称
字段,否则名称不能为空

有关详细信息,请参阅:


我纠正了我的问题,我的道歉即使是在更新之后,如果没有看到代码给你带来意想不到的结果,也很难说发生了什么。请参阅以了解如何使其更容易帮助您。我在上面添加了javasript代码,当我使用规则“//allow read:if resource.data.size()==0;”时,我可以检索所有文档,但我尝试从文档中访问“resource.data.name!=null”时失败,以节省您的时间和资源,Cloud Firestore根据查询的潜在结果集而不是所有文档的实际字段值来评估查询。这是否意味着当我们一次查询多个文档时,firestore安全规则不会计算每个文档的实际值?