【发布时间】:2021-03-24 22:00:21
【问题描述】:
我使用 MSAL 登录应用程序。登录后,我调用 Microsoft Graph API 来获取当前用户的电子邮件,然后我想将其传递给另一个 API。每次我尝试传递这些数据时,结果都是未定义的。
这是我在组件中从 Microsoft 调用 API 的方法。
userEmail: string | undefined;
getProfile(){
this.http.get(GRAPH_ENDPOINT)
.subscribe((profile: ProfileType) => {
this.userEmail = profile.userPrincipalName; //setting userEmail to the returned email address
console.log(this.userEmail); //logging the email address
});
}
然后,我在组件的ngOnInit 方法中编写:
ngOnInit(): void {
this.getProfile();
console.log(this.userEmail) //logging the returned email address
}
此控制台输出
undefined // the console log from ngOnInit from userEmail
test@microsoft.com // console log from http request
这是我一直遇到的错误,找不到解决方法。 我希望能够将 userEmail 设置为从 Microsoft Graph API 返回的帐户电子邮件地址,以便我可以在另一个 API 调用中使用它。
【问题讨论】:
标签: angular typescript api msal