亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

msal typescript error 屬性'accessToken'在類型'void

msal typescript error 屬性'accessToken'在類型'void

瀟湘沐 2022-09-02 20:56:41
下面的代碼為 生成以下 TypeScript 錯誤:response.accessTokenTS2339:屬性“accessToken”在類型“void |上不存在TokenResponse'.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 定義時,它清楚地表明該屬性在對象上可用:accessTokenexport type TokenResponse = AuthResponse & {    uniqueId: string;    tenantId: string;    scopes: Array<string>;    tokenType: string;    idToken: string;    idTokenClaims: StringDict;    accessToken: string;    refreshToken: string;    expiresOn: Date;    account: Account;};我做錯了什么?
查看完整描述

1 回答

?
慕妹3242003

TA貢獻1824條經驗 獲得超6個贊

只是一個關于你的,雖然你正在使用.我會這樣寫這段代碼:catchawait


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


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

    try {

        return await auth.acquireTokenSilent(request);

    } catch (error) {

        return await auth.acquireTokenPopup(request);

    }

}


const getGraphDetails = async (

    uri: string,

    scopes: Msal.TokenExchangeParameters,

    axiosConfig?: AxiosRequestConfig

) => {

    try {

        const response = await getTokenPopup(scopes);

        return callGraph(uri, response.accessToken, axiosConfig);

    } catch (error) {

        throw new Error("You could not get a token");

    }

}

現在,為什么要進入.有一種可能性,該函數將同時失敗, 對于 和 。因此,該函數將引發錯誤(或不返回任何內容,具體取決于您的實現)。voidresponsegetTokenPopupacquireTokenSilentacquireTokenPopupgetTokenPopup


TypeScript 看到這一點,并添加一個類型以指示有可能無法獲得響應。void


查看完整回答
反對 回復 2022-09-02
  • 1 回答
  • 0 關注
  • 129 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號