【发布时间】:2020-10-03 09:47:47
【问题描述】:
如何处理 firebase admin 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,
}
}
});
【问题讨论】:
-
如果此代码在可调用云函数中,请编辑您的问题以包含整个函数,以及您如何调用它。
-
已注明,已更改。
标签: javascript firebase firebase-authentication google-cloud-functions firebase-admin