2017-10-18 69 views
0

我想查詢的子集合中AngularFire2與沒有成功公司的FireStore。已搜索,但沒有找到合適的解決方案如何查詢AngularFire2/FireStore中的子集合?

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> 

我在這一個新手。

+0

你能用stackblitz.com重現問題?我可以幫助很多更用一個例子(我AngularFire的作者) –

+0

大衛嗨 - 擁有使用stackblitz困難,但我已經把我在github上https://github.com/JacquesSchalkwyk/Firestore-Qry嘗試。我希望這將有所幫助。目前我的數據庫有默認的規則,每個人都可以讀寫。結構是Country-Towns-Listings –

+0

當我將規則設置爲:-service cloud.firestore match/databases/{database}/documents {0} {0} }} } 時 –

回答

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); 
+0

我設定的規則來服務cloud.firestore { 匹配/數據庫/ {}數據庫/文件{ 匹配/國家/ {}國家/鎮/ {城市} { 允許讀,寫; }} } 我的第一個QRY運行,但第二個失敗,權限錯誤 –

+0

我已經更新了規則的例子。該版本仍然有權限錯誤嗎?如果沒有,還有其他錯誤? – Remi

+0

謝謝。將嘗試併發布結果。 –