我正在使用Firebase和AngularFire2库构建一个Angular2应用程序。
如何在用户注销授权连接后进行处理?例如,具有有效帐户的用户登录,与Firebase数据库的“订单”节点建立连接,然后用户注销。
我在控制台中收到以下错误,这非常有意义。但是我应该如何捕获此错误或以其他方式阻止它?
错误:
FIREBASE WARNING: Exception was thrown by user callback. Error: permission_denied at /orders: Client doesn't have permission to access the desired data.
相关代码(我认为):
@Injectable()
export class OrderService {
private orders$: FirebaseListObservable<any>;
private _pendingOrders$: BehaviorSubject<any> = new BehaviorSubject(null);
private _activeOrders$: BehaviorSubject<any> = new BehaviorSubject(null);
constructor(
private af: AngularFire,
private auth: AuthService) {
this.auth.isAuthed
.subscribe((value: boolean) => {
if (this.auth.isAuthed.value) {
const userId = this.auth.getUserId();
this._subscribeToUserOrders(userId);
} else {
// Somehow unsubscribe here, perhaps?
}
});
}
_subscribeToUserOrders(userId) {
const query = {
orderByChild: 'userId',
equalTo: userId
};
this.orders$ = this.af.database
.list(`orders`, query);
this.orders$.subscribe((orders) => {
// Load pending orders
this._pendingOrders$.next(orders.filter(o => o.status === 'PENDING'));
// Load active orders
this._activeOrders$.next(orders.filter(o => o.status === 'ACTIVE'));
});
}
get pendingOrders() {
return this._pendingOrders$.asObservable();
}
get activeOrders() {
return this._activeOrders$.asObservable();
}
}
答案 0 :(得分:1)
对this.orders$.subscribe
的调用将返回RxJS Subscription
:
import { Subscription } from 'rxjs/Subscription';
private ordersSubscription: Subscription;
...
this.ordersSubscription = this.orders$.subscribe(...);
您可以用来取消订阅(您可能也希望从您的主题中发出null
):
if (this.auth.isAuthed.value) {
const userId = this.auth.getUserId();
this._subscribeToUserOrders(userId);
} else {
this._unsubscribeFromUserOrders();
}
...
_unsubscribeFromUserOrders() {
this.ordersSubscription.unsubscribe();
this.orders$ = null;
this._pendingOrders$.next(null);
this._activeOrders$.next(null);
}