Angular 2 - Auth0用户配置文件,未定义

时间:2017-05-30 10:04:30

标签: angular undefined auth0

我尝试在Angular 2项目中使用Auth0。我正在从Auth0 website学习它。我的问题在于用户个人资料。登录后,我在ngOnInit上调用与网站示例中的方法大致相同的方法:

if (this.auth.isAuthenticated()) {
    if (this.auth.userProfile) {
        this.profile = this.auth.userProfile;
    } else {
        this.auth.getProfile((err, profile) => {
            this.profile = profile;
        });
    }

    this.auth.alreadyExists(this.profile.sub);
}

问题在于this.auth.alreadyExists(this.profile.sub);方法的参数this.profile.sub

错误消息是:

错误错误:未捕获(在承诺中):TypeError:无法读取未定义的属性“sub” TypeError:无法读取未定义的属性“sub”。

如果我在HTML文件{{profile?.sub}}中写入并删除this.auth.alreadyExists(this.profile.sub);方法,则会显示user_id没有问题。

我不知道错误在哪里。

谢谢!

1 个答案:

答案 0 :(得分:2)

由于getProfile是异步的,因此当您调用alreadyExists时,profile.sub尚不存在。如果您将对hasExists的调用移动到getProfile回调中,那么您应该没问题。

相关问题