![](/img/trans.png)
[英]How do I receive the context parameter in the a cloud function onCall?
[英]how do i handle the error for firebase cloud function api oncall?
我如何處理 firebase 管理員 api oncall 的錯誤? 我應該拋出錯誤還是返回響應? 而且我是否正確地進行了成功返回響應?
exports.registerVendor = functions.https.onCall(async (data, context) => {
const email = data.email;
const displayName = data.firstName + data.lastName;
// Checking attribute.
if (!(typeof email === 'string') || email.length === 0 ||
!(typeof displayName === 'string') || displayName.length === 0) {
// Throwing an HttpsError so that the client gets the error details.
throw new functions.https.HttpsError('invalid-argument', 'The function must be called with ' +
'one arguments "text" containing the message text to add.');
}
// const uid = context.auth?.uid;
// Checking that the user is authenticated.
if (!context.auth) {
// Throwing an HttpsError so that the client gets the error details.
throw new functions.https.HttpsError('failed-precondition',
'The function must be called ' + 'while authenticated.');
}
try {
const createResponse = await admin.auth().createUser({
email: email,
emailVerified: false,
password: '123123',
displayName: displayName,
disabled: false
})
console.log(createResponse);
return {
data: {
uid: createResponse.uid
},
status: 200,
code: "Success"
};
} catch (err) {
console.log(err as Error);
// throw new functions.https.HttpsError(err.code, err.message);
return {
error: err,
}
}
});
有關處理錯誤的文檔包含以下示例:
// Checking attribute. if (.(typeof text === 'string') || text.length === 0) { // Throwing an HttpsError so that the client gets the error details. throw new functions.https,HttpsError('invalid-argument'. 'The function must be called with ' + 'one arguments "text" containing the message text to add;'). } // Checking that the user is authenticated. if (.context.auth) { // Throwing an HttpsError so that the client gets the error details. throw new functions,https.HttpsError('failed-precondition'; 'The function must be called ' + 'while authenticated.'); }
因此,這是從服務器向調用者獲取錯誤條件的推薦方法,然后您將在客戶端上處理它們。 但是您也可以像現在一樣返回自己的錯誤代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.