【问题标题】:How to query subcollections in AngularFire2/FireStore?如何在 AngularFire2/FireStore 中查询子集合?
【发布时间】:2017-10-18 07:57:25
【问题描述】:

我正在尝试使用 FireStore 查询 AngularFire2 中的子集合,但没有成功。已搜索但没有找到合适的解决方案。

ngOnInit() {
// this does work
this.africaCollection = this.afs.collection('Country', ref => ref
.where('code', '==', 'za'));
this.countriesAfrica = this.africaCollection.valueChanges();

// this does not work
this.townCollection = this.afs.collection('Country').doc('za')                         
.collection('Towns');
this.towns = this.townCollection.valueChanges();
{

我的 HTML 如下所示:

<div>
  <table>
    <tr *ngFor="let town of towns | async">
      <td>{{ town.name }}</td>
    </tr>
  </table>
</div>

我是这方面的新手。

【问题讨论】:

  • 您可以使用 stackblitz.com 重新创建问题吗?我可以通过一个示例提供更多帮助(我是 AngularFire 的作者)
  • 嗨,David - 使用 stackblitz 有困难,但我已将我的尝试放在 github github.com/JacquesSchalkwyk/Firestore-Qry。我希望这将有所帮助。目前我的数据库有默认规则,每个人都可以读写。结构是 Country-Towns-Listings
  • 当我将规则设置为 :-service cloud.firestore { match /databases/{database}/documents { match /Country/{country}{ allow read, write; } } }

标签: angularfire2 google-cloud-firestore


【解决方案1】:

可能和你的规则有关。

默认规则

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write;
    }
  }
}

这里有更多关于规则的细节:

https://firebase.google.com/docs/firestore/security/secure-data#rules_dont_cascade

确保您在该集合中有项目

您还可以通过代码将项目添加到您的收藏中,以确保其位置正确。然后,您就可以查看您的 Firebase 控制台以了解它的放置位置。

var town: Town = {
  name: "town name",
  region: "region name"
};
this.afs.collection('Country').doc('za').collection('Towns').add(town);

【讨论】:

  • 当我将规则设置为服务 cloud.firestore { match /databases/{database}/documents { match /Country/{country}/Towns/{towns} { 允许读取,写入; } } } 我的第一个 qry 运行,但第二个因权限错误而失败
  • 我已经更新了规则示例。您仍然会收到该版本的权限错误吗?如果没有,还有其他错误吗?
  • 谢谢。将尝试发布结果。
  • 仍然报告缺少权限或权限不足。这会帮助github.com/JacquesSchalkwyk/Firestore-Qry
  • 重新运行:现在没有错误,但现在仍然是城镇。
猜你喜欢
  • 1970-01-01
  • 2018-07-26
  • 1970-01-01
  • 2019-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多