我使用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