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











up vote
1
down vote

favorite












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




























    up vote
    1
    down vote

    favorite












    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


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      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 at 21:50

























      asked Nov 19 at 16:41









      Kenny

      7333936




      7333936
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          "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 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 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',
          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








          up vote
          0
          down vote













          "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 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 at 18:56

















          up vote
          0
          down vote













          "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 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 at 18:56















          up vote
          0
          down vote










          up vote
          0
          down vote









          "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 at 17:52









          Doug Stevenson

          65.9k77997




          65.9k77997












          • 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 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 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 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 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 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 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 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 at 18:56




















           

          draft saved


          draft discarded



















































           


          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







          Popular posts from this blog

          Feedback on college project

          Futebolista

          Albești (Vaslui)