use*_*539 4 observable firebase firebase-realtime-database angularfire2 angular
我在这里使用AngularFire2版本的代码,我用它来从我的一个组件上的数据库中获取一些成员.
当我在同一个组件中调用此注销时,我遇到了问题.我取消订阅订阅但我仍然在控制台中收到错误说Exception was thrown by user callback. Error: 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'));
}
Run Code Online (Sandbox Code Playgroud)
您可以尝试使用RxJS主题以及运算符takeUntil()来帮助确保在以下情况之前清除observable 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'));
}
}
Run Code Online (Sandbox Code Playgroud)
希望这有帮助!
| 归档时间: |
|
| 查看次数: |
1361 次 |
| 最近记录: |