【问题标题】:Angular/Ionic RXJS pipe/map undefined element on reload重新加载时的角度/离子RXJS管道/映射未定义元素
【发布时间】:2019-11-06 17:37:17
【问题描述】:

简单的管理员角色检查方法:

 isAdmin() {
    return this.userProfileObservable.pipe(map((profile: UserObject) => {
//Here I can place some delaying code and it works!!!
        return profile != null ? profile.role === 'admin' : false;
    }));
}

如果我从上一页导航到该页面并路由器调用 AdminGuard,则效果很好:

 canActivate(
    next: ActivatedRouteSnapshot,
    state: RouterStateSnapshot): Observable<boolean> {
    return this.auth.isAdmin();
}

Observables 在服务构造函数中初始化:

private _userProfileSubject: BehaviorSubject<UserObject>;
private _firebaseUserSubject: BehaviorSubject<firebase.User>;
private userProfileCollection: AngularFirestoreDocument<UserObject>;
public userProfileObservable: Observable<UserObject>;
private authObserver: firebase.Unsubscribe;

constructor(
    private afAuth: AngularFireAuth,
    private afs: AngularFirestore, ) {
    // Init observers with null initial value
    this._userProfileSubject = new BehaviorSubject(new UserObject()) as BehaviorSubject<UserObject>;
    this._firebaseUserSubject = new BehaviorSubject(null) as BehaviorSubject<firebase.User>;
    this.userProfileObservable = this._userProfileSubject.asObservable();

    this.authObserver = afAuth.auth.onAuthStateChanged((user) => {
        this._firebaseUserSubject.next(user);

        if (user == null) {
            this._userProfileSubject.next(null);
            return;
        }
        this.userProfileCollection = afs.collection<UserObject>('Users').doc(user.uid);
        // Monitor auth changes and update behaviorSubject -> observable <UserObject>
        this.userProfileCollection.snapshotChanges().subscribe((action => {
            const data = action.payload.data() as UserObject;
            data.uid = action.payload.id;
            this._userProfileSubject.next(data);
            return data;
        }));
    });
}

但是如果我重新加载页面/直接导航到受 AdminGuard 保护的路由,管道中的属性/元素 -> 映射 -> “配置文件”由于某种原因变得未定义。

好像还没有加载所有内容,就好像我在 isAdmin 方法中执行“BIG HACK”并等待/延迟之前,然后它再次工作......

【问题讨论】:

    标签: angular service pipe observable reload


    【解决方案1】:

    您能否进行以下更改并尝试:

    在您的服务中,让 BehavorSubject 初始化为 null;

    this._userProfileSubject: BehaviorSubject<UserObject> = new BehaviorSubject(null);
    

    然后更新您的 isAdmin() 方法,使其忽略 userProfileObservable 的第一个“空”值并等待它发出 NOT NULL 值 [为此使用 skipWhile() 运算符],如下所示:

    isAdmin() {
                return this.userProfileObservable
                           .pipe(
                            //it will skip all null value until source sobservable emits a NOT NULL value; i.e. it will wait until your service's subscribe get the response and emits a NOT NULL value of UserObject
                            skipWhile(u => !u),
                            map((profile: UserObject) => {
                              //Here I can place some delaying code and it works!!!
                              return profile != null ? profile.role === 'admin' : false;
                            })
                          );
            }
    

    【讨论】:

      猜你喜欢
      • 2012-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-13
      • 2019-05-09
      • 1970-01-01
      • 1970-01-01
      • 2021-02-02
      相关资源
      最近更新 更多