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

Angular 2-Auth0用户配置文件,未定义,angular,undefined,auth0,Angular,Undefined,Auth0,我尝试在Angular 2项目中使用Auth0。我正在学习它的形式。我的问题是用户配置文件。登录后,我调用ngOnInit的方法与网站示例中的方法大致相同: if (this.auth.isAuthenticated()) { if (this.auth.userProfile) { this.profile = this.auth.userProfile; } else { this.auth.getProfile((err, profile)

我尝试在Angular 2项目中使用Auth0。我正在学习它的形式。我的问题是用户配置文件。登录后,我调用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)
method,它可以毫无问题地显示用户id

我不知道哪里出了错


谢谢大家!

因为getProfile是异步的,所以调用alreadyExists时profile.sub还不存在。如果您将对alreadyExists的调用向上移动到getProfile回调中,那么您应该不会有问题

我现在还有一个问题。我已经将alreadyExists方法放在getProfile方法中,现在我得到了以下错误消息:无法读取未定义的“userId”的属性“userId”是我的模型的属性。我需要查看您的
alreadyExists
方法。最好再贴一个问题:)