![](/img/trans.png)
[英]Object is possibly 'undefined' and Cannot invoke an object which is possibly 'undefined'
[英]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.