[英]Google Cloud Function - Storage - Delete Image - “ApiError: Error during request”
UPDATED QUESTION 更新的问题
The problem is ApiError: Error during request
. 问题是ApiError: Error during request
。
Code: 码:
import * as functions from 'firebase-functions';
const cors = require('cors')({ origin: true });
import * as admin from 'firebase-admin';
const gcs = admin.storage();
export const deleteImage = functions.https.onRequest((req, res) => {
return cors(req, res, async () => {
res.set('Content-Type', 'application/json');
const id = req.body.id;
const name = req.body.name;
const imageRef = gcs.bucket(`images/${name}`);
if (!name || !id) {
return res.status(400).send({message: 'Missing parameters :/'});
}
try {
await imageRef.delete();
console.log('Image deleted from Storage');
return res.status(200).send({status: 200, message: `Thank you for id ${id}`});
}
catch (error) {
console.log('error: ', error);
return res.status(500).send({message: `Image deletion failed: ${error}`});
}
});
});
And the problem is here : await imageRef.delete();
问题在这里 : await imageRef.delete();
, I get the following error: ,出现以下错误:
ApiError: Error during request. ApiError:请求期间出错。
I do, indeed, have admin.initializeApp();
我确实有admin.initializeApp();
in one of my other functions, so that can't be the issue, unless GCF have a bug. 除非我的功能有问题,否则这不是问题。
More In-Depth Error : 更多深度错误 :
{ ApiError: Error during request.
at Object.parseHttpRespBody (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/common/src/util.js:187:32)
at Object.handleResp (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/common/src/util.js:131:18)
at /user_code/node_modules/firebase-admin/node_modules/@google-cloud/common/src/util.js:496:12
at Request.onResponse [as _callback] (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/common/node_modules/retry-request/index.js:198:7)
at Request.self.callback (/user_code/node_modules/firebase-admin/node_modules/request/request.js:185:22)
at emitTwo (events.js:106:13)
at Request.emit (events.js:191:7)
at Request.<anonymous> (/user_code/node_modules/firebase-admin/node_modules/request/request.js:1161:10)
at emitOne (events.js:96:13)
at Request.emit (events.js:188:7)
code: undefined,
errors: undefined,
response: undefined,
message: 'Error during request.' }
(old question removed) (旧问题已删除)
"Error: Can't set headers after they are sent" means that you tried to send two responses to the client. “错误:发送标头后无法设置标头”表示您尝试向客户端发送两个响应。 This isn't valid - you can send only one response. 这是无效的-您只能发送一个回复。
Your code is clearly sending two 200 type responses to the client in the event that imageRef.delete() fails and the catch callback on it is triggered. 如果imageRef.delete()失败并触发了catch回调,则您的代码显然会向客户端发送两个200类型的响应。
Also, you're mixing up await with then/catch. 另外,您还在混接then / catch。 They're not meant to be used together. 它们不打算一起使用。 You choose one or the other. 您选择一个。 Typically, if you're using await for async programming, you don't also use then/catch with the same promise. 通常,如果您正在使用await进行异步编程,则不要同时使用具有相同承诺的then / catch。 This is more idiomatic use of await with error handling: 这是带有错误处理的await的更多惯用用法:
try {
await imageRef.delete()
res.status(200).send({status: 200, message: `Thank you for id ${id}`});
} catch (error) {
res.status(500).send({message: `Image deletion failed: ${err}`});
}
Note also that you typically send a 500 response to the client on failure, not 200, which indicates success. 还要注意,通常在失败时向客户端发送500响应,而不是200,这表示成功。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.