简体   繁体   English

Google云功能-存储-删除图像-“ ApiError:请求期间出错”

[英]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.

相关问题 Google 云存储 ApiError:必需 - Google cloud Storage ApiError: Required 无法使用云删除 Firebase 存储映像 Function - Unable to delete Firebase Storage Image with Cloud Function 如何解决“未捕获的 ApiError 错误:项目项目名称已被删除。” Google Cloud function 开发的 VSCode 问题? - How to resolve "Uncaught ApiError Error: Project project-name has been deleted." issue in VSCode for Google Cloud function development? 将图像从Google Cloud功能上传到云端存储 - Upload Image from Google Cloud Function to Cloud Storage Google云端存储“无效的上传请求”错误。 错误的请求 - Google Cloud Storage “invalid upload request” error. Bad request Google Cloud 存储不是一项功能 - Google Cloud storage is not a function 从谷歌云存储下载图像并在请求中发送失败 - download image from google cloud storage and send it in a request fails 谷歌云存储的 firebase 云函数 API 错误 - firebase cloud function API Error with Google Cloud Storage Express + Multer + Google Cloud Storage 错误:“需要回调函数” - Express + Multer + Google Cloud Storage Error: “Requires a callback function” 使用Google Cloud AutoML模型预测Firebase功能存储在Google Cloud存储中的图像 - Use Google Cloud AutoML model predict an image which is stored in Google Cloud storage in Firebase function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM