错误TypeError:注销时“用户为空”

时间:2019-03-29 10:20:06

标签: angular

嗨,我使用的是Angle 7,我用它来查看照片:

this._authService.getAuthStatusListener()
    .pipe(takeUntil(this._unsubscribeAll))
    .subscribe((user) => {
        this.user = user;
        console.log("user", user.thumbnail.encoded)
        if (user.thumbnail.encoded) {
            this.photo = this.sanitizer.bypassSecurityTrustUrl("data:image/png;" + user.thumbnail.encoding + ", " + user.thumbnail.encoded);
        } else {
            this.photo = "assets/images/avatars/user.jpg"
        }

    });

这是html:

<div *ngIf="user" class="user" fxLayout="column" [ngClass]="fuseConfig.layout.navbar.secondaryBackground">

    <div *ngIf="user" class="h3 username">{{ user.first_name }} {{ user.last_name }}</div>
    <div class="h5 company hint-text mt-8">{{ user.company }}</div>
    <div class="avatar-container" [ngClass]="fuseConfig.layout.navbar.primaryBackground">
        <img class="avatar"  src="assets/images/avatars/user.jpg">
    </div>

</div>

当我退出应用程序时,我的控制台中出现此错误

错误TypeError:“用户为空”

1 个答案:

答案 0 :(得分:0)

您执行:

console.log("user", user.thumbnail.encoded)

但是,当您注销时,用户为null,因此您可以访问user.thumbnail.encoded 解决方案:

    if (user && user.thumbnail && user.thumbnail.encoded ) {
        console.log("user", user.thumbnail.encoded)
        this.photo = this.sanitizer.bypassSecurityTrustUrl("data:image/png;" + user.thumbnail.encoding + ", " + user.thumbnail.encoded);
    } else {
        this.photo = "assets/images/avatars/user.jpg"
    }