【问题标题】:Angular 2 - Auth0 User Profile, undefinedAngular 2 - Auth0 用户配置文件,未定义
【发布时间】:2017-05-31 01:13:16
【问题描述】:

我尝试在我的 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:无法读取未定义的属性“子” 类型错误:无法读取未定义的属性“子”。

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

我不知道错在哪里。

谢谢!

【问题讨论】:

    标签: angular undefined auth0


    【解决方案1】:

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

    【讨论】:

    • 我现在还有一个问题。我已将 alreadyExists 方法放在 getProfile 方法中,现在我收到此错误消息:无法读取未定义的属性 'userId'
    • 'userId' 是我的模型的属性
    • 我需要看看你的alreadyExists 方法。最好发布另一个问题:)
    猜你喜欢
    • 1970-01-01
    • 2018-08-27
    • 1970-01-01
    • 1970-01-01
    • 2019-04-01
    • 2018-10-16
    • 2018-07-21
    • 2016-05-03
    • 2018-01-01
    相关资源
    最近更新 更多