【问题标题】:How to query firebase in readonly mode如何以只读模式查询firebase
【发布时间】:2020-03-30 21:17:58
【问题描述】:

我在 Firebase 中有一些我想查询的数据。我的 Firebase 具有只读的默认安全设置。但是,当我尝试如下查询数据时

this.firestore.collection('myCollection').valueChanges().pipe().subscribe(x => {
  console.log;
});

this.firestore.collection('myCollection').get().subscribe(x => {
  console.log;
});

我确实收到了一个错误Missing or insufficient permissions. 根据我的理解,这不应该是这种情况,因为我只是在读取数据。另外我可能不得不提到我正在使用@angular/fire

只是为了一个实验,我尝试启用对 数据库,它以这种方式工作,但我想保持只读模式。

这是我的规则:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if false;
    }
  }
}

有什么想法吗?

【问题讨论】:

  • 你能分享你的安全规则吗?
  • 如果您在安全规则中正确启用了 myCollection 的读取访问权限,您的代码应该可以工作。所以,你的规则有问题。
  • @DougStevenson 刚刚添加了规则,正如我所说的,这只是一个默认值,对我来说似乎很好。

标签: firebase google-cloud-firestore angularfire


【解决方案1】:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if false;
    }
  }
}

您不允许只读访问,因为它等同于

....
allow read: if false;   // <- You deny any read here
allow write: if false;
....

你需要调整你的规则如下:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read: if true;
      allow write: if false;
    }
  }
}

doc 中的更多信息,包括文档顶部的视频。

【讨论】:

  • 哦,好吧,我没有这样想。
猜你喜欢
  • 1970-01-01
  • 2021-08-16
  • 1970-01-01
  • 1970-01-01
  • 2018-02-09
  • 1970-01-01
  • 1970-01-01
  • 2012-09-29
  • 1970-01-01
相关资源
最近更新 更多