msal typescript error 属性'accessToken'在类型'void

下面的代码为 生成以下 TypeScript 错误:response.accessToken

TS2339:属性“accessToken”在类型“void |上不存在TokenResponse'.

http://img3.mukewang.com/6311fda60001378307700143.jpg

import * as Msal from '@azure/msal-browser'


export async function getTokenPopup(request: Msal.TokenExchangeParameters) {

  return await auth.acquireTokenSilent(request).catch(async () => {

    return await auth.acquireTokenPopup(request).catch((error) => {

      console.log('login with popup failed: ', error)

    })

  })

}


const getGraphDetails = async (

  uri: string,

  scopes: Msal.TokenExchangeParameters,

  axiosConfig?: AxiosRequestConfig

) => {

  return getTokenPopup(scopes).then((response) => {

      return callGraph(uri, response.accessToken, axiosConfig)

  })

}

当检查 TokenResponse 的 TS 定义时,它清楚地表明该属性在对象上可用:accessToken


export type TokenResponse = AuthResponse & {

    uniqueId: string;

    tenantId: string;

    scopes: Array<string>;

    tokenType: string;

    idToken: string;

    idTokenClaims: StringDict;

    accessToken: string;

    refreshToken: string;

    expiresOn: Date;

    account: Account;

};

我做错了什么?


潇湘沐
浏览 99回答 1
1回答

慕妹3242003

只是一个关于你的,虽然你正在使用.我会这样写这段代码:catchawaitimport * as Msal from '@azure/msal-browser'export async function getTokenPopup(request: Msal.TokenExchangeParameters) {&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; return await auth.acquireTokenSilent(request);&nbsp; &nbsp; } catch (error) {&nbsp; &nbsp; &nbsp; &nbsp; return await auth.acquireTokenPopup(request);&nbsp; &nbsp; }}const getGraphDetails = async (&nbsp; &nbsp; uri: string,&nbsp; &nbsp; scopes: Msal.TokenExchangeParameters,&nbsp; &nbsp; axiosConfig?: AxiosRequestConfig) => {&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; const response = await getTokenPopup(scopes);&nbsp; &nbsp; &nbsp; &nbsp; return callGraph(uri, response.accessToken, axiosConfig);&nbsp; &nbsp; } catch (error) {&nbsp; &nbsp; &nbsp; &nbsp; throw new Error("You could not get a token");&nbsp; &nbsp; }}现在,为什么要进入.有一种可能性,该函数将同时失败, 对于 和 。因此,该函数将引发错误(或不返回任何内容,具体取决于您的实现)。voidresponsegetTokenPopupacquireTokenSilentacquireTokenPopupgetTokenPopupTypeScript 看到这一点,并添加一个类型以指示有可能无法获得响应。void
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript