【发布时间】:2020-11-25 12:55:16
【问题描述】:
我已经实现了一个 HTTPs (onCall) 函数,如果工作成功完成,它会向客户端抛出一些错误或返回 true。问题是我不明白为什么要返回 true(因为当我抛出错误时我不返回 false)。
由于HTTP协议要求向客户端返回响应以完成请求,我应该向客户端返回什么?我正在考虑删除我抛出的错误并返回一个经典的 HTTP 响应(状态代码、正文......)。
有什么想法吗?这是我正在做的事情:
exports.function = functions
.region("us-central1")
.runWith({ memory: "2GB", timeoutSeconds: 120 })
.https.onCall(async (data, context) => {
// Lazy initialization of the Admin SDK
if (!is_function_initialized) {
// ... stuff
is_uploadImage_initialized = true;
}
// ... asynchronous stuff
// When all promises has been resolved...
// If work completed successfully
return true;
/*
Is it correct instead ???
return {code: "200 OK", date: date, body: message };
*/
// Else, if errors
throw new Error("Please, try again later.");
/*
Is it correct instead ???
return {code: "418 I'm a teapot", date: date, body: message };
*/
}
【问题讨论】:
标签: javascript firebase google-cloud-platform google-cloud-functions