簡體   English   中英

Firebase 自定義聲明返回對象可能是“未定義”

[英]Firebase custom claims returns Object is possibly 'undefined'

我正在嘗試閱讀用戶的自定義聲明,並且我想將聲明的值單獨返回給客戶。

這段代碼已經工作了幾個月。 但是,它突然返回錯誤“對象可能未定義”。

此代碼出現在以下幾行中:

role: userRecord.customClaims["role"],
type: userRecord.customClaims["type"],

這是整個代碼片段:

exports.checkAuthClaim = functions.https.onCall(async (data, context) => {
  const userId = data.userId;
  return admin
      .auth()
      .getUser(userId)
      .then((userRecord) => {
        console.log(userRecord.customClaims);
        return {
          role: userRecord.customClaims["role"],
          type: userRecord.customClaims["type"],
        };
      });
});

任何幫助表示贊賞:)

用戶可能未定義,如果您使用無效的用戶 ID,可能會發生這種情況。 使用可選鏈接來消除此錯誤。

        return {
          role: userRecord?.customClaims?.["role"],
          type: userRecord?.customClaims?.["type"],
        };

只需檢查未定義的

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM