gpt4 book ai didi

AngularFire2 在注销时取消订阅,在未关闭订阅时出现数据库权限错误

转载 作者:太空狗 更新时间:2023-10-29 17:49:56 26 4
gpt4 key购买 nike

我这里有这段代码使用 AngularFire2 版本,我用它从我的组件之一的数据库中获取一些成员。

当我在同一组件中调用此注销时遇到问题。我取消订阅,但我仍然在控制台中收到错误消息,提示 Exception was thrown by user callback。错误:permission_denied at/users/uid。我知道这是因为我不再登录并且我的数据库规则不允许此读取操作。我不知道为什么我已经退订了,它还在尝试读取数据。

    constructor(private afAuth: AngularFireAuth, private db: AngularFireDatabase) {
this.user = afAuth.authState;

this.userSub = this.user.subscribe(auth => {
if (auth) {
this.member = db.object('/users/' + auth.uid);
this.dbSub = this.member.subscribe();
}
});
}

logout() {
this.dbSub.unsubscribe();
this.userSub.unsubscribe();

this.afAuth.auth.signOut()
.then(() => this.router.navigateByUrl('/login'));
}

最佳答案

您或许可以尝试将 RxJS Subject 与运算符 takeUntil() 一起使用,以帮助确保在 signOut() 之前清理可观察对象:

import { Subject } from 'rxjs/Subject';
import 'rxjs/add/operator/takeUntil';

export class Foo {
private ngUnsubscribe: Subject<void> = new Subject<void>();

constructor(private afAuth: AngularFireAuth, private db: AngularFireDatabase) {
this.user = afAuth.authState;

this.userSub = this.user.subscribe(auth => {
if (auth) {
this.member = db.object('/users/' + auth.uid);

this.dbSub = this.member
.takeUntil(ngUnsubscribe)
.subscribe(results => console.log(results));
}
});
}

logout() {
this.ngUnsubscribe.next();
this.ngUnsubscribe.complete();

this.userSub.unsubscribe();

this.afAuth.auth.signOut()
.then(() => this.router.navigateByUrl('/login'));
}
}

关于AngularFire2 在注销时取消订阅,在未关闭订阅时出现数据库权限错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45245440/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com