Google Cloud Function - Storage - Delete Image - “ApiError: Error during request”












1















UPDATED QUESTION



The problem is 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();, I get the following error:




ApiError: Error during request.




I do, indeed, have 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)










share|improve this question





























    1















    UPDATED QUESTION



    The problem is 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();, I get the following error:




    ApiError: Error during request.




    I do, indeed, have 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)










    share|improve this question



























      1












      1








      1








      UPDATED QUESTION



      The problem is 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();, I get the following error:




      ApiError: Error during request.




      I do, indeed, have 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)










      share|improve this question
















      UPDATED QUESTION



      The problem is 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();, I get the following error:




      ApiError: Error during request.




      I do, indeed, have 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)







      node.js firebase google-cloud-storage google-cloud-functions






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 19 '18 at 21:50







      Kenny

















      asked Nov 19 '18 at 16:41









      KennyKenny

      76031037




      76031037
























          1 Answer
          1






          active

          oldest

          votes


















          0














          "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.



          Also, you're mixing up await with 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. This is more idiomatic use of await with error handling:



          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.






          share|improve this answer
























          • Hi Doug, thanks for your response. This are definitely some good improvements to go into my code, however, the primary error still has no solution: "ApiError: Error during request" Any ideas behind this?

            – Kenny
            Nov 19 '18 at 18:45











          • Then I would suggest reframing your question with the shortest possible code in a single file that demonstrates the problem, so that other issues won't get conflated, and we won't have to take your word that missing code is actually present.

            – Doug Stevenson
            Nov 19 '18 at 18:56













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53379131%2fgoogle-cloud-function-storage-delete-image-apierror-error-during-request%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          "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.



          Also, you're mixing up await with 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. This is more idiomatic use of await with error handling:



          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.






          share|improve this answer
























          • Hi Doug, thanks for your response. This are definitely some good improvements to go into my code, however, the primary error still has no solution: "ApiError: Error during request" Any ideas behind this?

            – Kenny
            Nov 19 '18 at 18:45











          • Then I would suggest reframing your question with the shortest possible code in a single file that demonstrates the problem, so that other issues won't get conflated, and we won't have to take your word that missing code is actually present.

            – Doug Stevenson
            Nov 19 '18 at 18:56


















          0














          "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.



          Also, you're mixing up await with 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. This is more idiomatic use of await with error handling:



          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.






          share|improve this answer
























          • Hi Doug, thanks for your response. This are definitely some good improvements to go into my code, however, the primary error still has no solution: "ApiError: Error during request" Any ideas behind this?

            – Kenny
            Nov 19 '18 at 18:45











          • Then I would suggest reframing your question with the shortest possible code in a single file that demonstrates the problem, so that other issues won't get conflated, and we won't have to take your word that missing code is actually present.

            – Doug Stevenson
            Nov 19 '18 at 18:56
















          0












          0








          0







          "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.



          Also, you're mixing up await with 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. This is more idiomatic use of await with error handling:



          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.






          share|improve this answer













          "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.



          Also, you're mixing up await with 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. This is more idiomatic use of await with error handling:



          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 19 '18 at 17:52









          Doug StevensonDoug Stevenson

          76.9k990111




          76.9k990111













          • Hi Doug, thanks for your response. This are definitely some good improvements to go into my code, however, the primary error still has no solution: "ApiError: Error during request" Any ideas behind this?

            – Kenny
            Nov 19 '18 at 18:45











          • Then I would suggest reframing your question with the shortest possible code in a single file that demonstrates the problem, so that other issues won't get conflated, and we won't have to take your word that missing code is actually present.

            – Doug Stevenson
            Nov 19 '18 at 18:56





















          • Hi Doug, thanks for your response. This are definitely some good improvements to go into my code, however, the primary error still has no solution: "ApiError: Error during request" Any ideas behind this?

            – Kenny
            Nov 19 '18 at 18:45











          • Then I would suggest reframing your question with the shortest possible code in a single file that demonstrates the problem, so that other issues won't get conflated, and we won't have to take your word that missing code is actually present.

            – Doug Stevenson
            Nov 19 '18 at 18:56



















          Hi Doug, thanks for your response. This are definitely some good improvements to go into my code, however, the primary error still has no solution: "ApiError: Error during request" Any ideas behind this?

          – Kenny
          Nov 19 '18 at 18:45





          Hi Doug, thanks for your response. This are definitely some good improvements to go into my code, however, the primary error still has no solution: "ApiError: Error during request" Any ideas behind this?

          – Kenny
          Nov 19 '18 at 18:45













          Then I would suggest reframing your question with the shortest possible code in a single file that demonstrates the problem, so that other issues won't get conflated, and we won't have to take your word that missing code is actually present.

          – Doug Stevenson
          Nov 19 '18 at 18:56







          Then I would suggest reframing your question with the shortest possible code in a single file that demonstrates the problem, so that other issues won't get conflated, and we won't have to take your word that missing code is actually present.

          – Doug Stevenson
          Nov 19 '18 at 18:56






















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53379131%2fgoogle-cloud-function-storage-delete-image-apierror-error-during-request%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud

          Zucchini