我使用Ionic2
與Angularfire2
訪問Firebase Authentication
。Firebase如何取消訂閱
我訪問以下rxjs/Observable
:
chats.ts
this.firelist = this.dataService.findChats();
this.subscription = this.firelist.subscribe(chatItems => {
...
});
ngDestroy() {
this.subscription.unsubscribe();
}
dataService.ts
findChats(): Observable<any[]> {
this.firebaseDataService.findChats().forEach(firebaseItems => {
...
observer.next(mergedItems);
});
}
firebaseDataService.ts
findChats(): Observable<any[]> { // populates the firelist
return this.af.database.list('/chat/', {
query: {
orderByChild: 'negativtimestamp'
}
}).map(items => {
const filtered = items.filter(
item => (item.memberId1 === this.me.uid || item.memberId2 === this.me.uid)
);
return filtered;
});
}
另外,在火力地堡認證,我有以下實時數據庫規則:
"rules": {
".read": "auth != null",
".write": "auth != null",
}
問題
這工作完全在用戶已登錄(即auth != null
),但只要用戶註銷,並auth == null
,我收到以下錯誤:
ERROR Error: Uncaught (in promise): Error: permission_denied at /chat: Client doesn't have permission to access the desired data. Error: permission_denied at /chat: Client doesn't have permission to access the desired data.
問題
正如你可以在上面的代碼中看到,我嘗試unsubscribe
從Firebase身份驗證服務,但必須做到不正確。如果有人可以建議我應該如何阻止我的應用查詢Firebase數據庫unsubscribe
,我將不勝感激。
你爲什麼不''this.firelist.unsubscribe()' – Skeptor
嗨Skeptor,感謝您的反饋。 'this.firelist'是一個'rxjs/Observable',並且api沒有'unsubscribe'功能。但是,它返回一個'訂閱',確實有一個,所以這是我沒有成功地嘗試,如上所示。 http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html –
Richard
我從不取消訂閱,所以不確定語法。但在我看來,你需要爲每個http調用觀看auth對象狀態,而不是取消訂閱。我將提供一種不同的方式來編寫相同的代碼,以及我在應用程序中的做法。 (我使用aws,但它們幾乎相同) – Skeptor