By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
How to set the dropdown value by clicking on a table row
Javascript – How to set the dropdown value by clicking on a table row
JavaScript
Attempting to increase the counter, when the object's tag exist
Javascript – Attempting to increase the counter, when the object’s tag exist
JavaScript
Cycle2 JS center active slide
Javascript – Cycle2 JS center active slide
JavaScript
Can import all THREE.js post processing modules as ES6 modules except OutputPass
Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
JavaScript
How to return closest match for an array in Google Sheets Appscript
Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript > Javascript – Error when uploading a picture Firebase: “TypeError [ERR_INVALID_ARG_VALUE]”
JavaScript

Javascript – Error when uploading a picture Firebase: “TypeError [ERR_INVALID_ARG_VALUE]”

Admin
Last updated: 2023/12/13 at 5:57 PM
Admin
Share
2 Min Read
Error when uploading a picture Firebase: "TypeError [ERR_INVALID_ARG_VALUE]"

Problem:

I’m facing an issue while trying to upload a picture using the following code in my Node.js application:

Contents
Problem:Solution:
// Controller function for uploading pictures
const uploadPicture = async (req, res) => {
  try {
    const { xId } = req.params;
    const file = req.file;

    if (!file) {
      return res.status(400).send("No file uploaded.");
    }

    // Define the bucket file name
    const bucketFileName = `x/${xId}/${file.originalname}`;

    await bucket.upload(file.buffer, {
      gzip: true,
      destination: bucketFileName, // Use the defined bucketFileName
      metadata: {
        cacheControl: "public, max-age=31536000",
        contentType: file.mimetype,
      },
    });

    const pictureData = {
      x: xId,
      path: `gs://${bucket.name}/${bucketFileName}`,
    };

    // Save the picture data to the Firestore database
    await db
      .collection("users")
      .doc("x")
      .update({
        comments: admin.firestore.FieldValue.arrayUnion(pictureData),
      });

    res.status(200).send("Picture uploaded successfully.");
  } catch (error) {
    console.error("Error:", error);
    res.status(500).send("Internal Server Error");
  }
};

I’m getting the following error message in my application:

Error: TypeError [ERR_INVALID_ARG_VALUE]: The argument ‘path’ must be a string or Uint8Array without null bytes. Received <Buffer …>

(Console log of file is also included in the error description)

It seems that this error is related to the “path” argument when trying to upload a picture. The file object contains the following information:

{
  fieldname: 'file',
  originalname: 'example-preview(11).png',
  encoding: '7bit',
  mimetype: 'image/png',
  buffer: <Buffer 89 50 4e 47 ... 10133 more bytes>,
  size: 10133
}

I’m not sure why this error is occurring, and I would appreciate any insights or suggestions on how to resolve this issue.

Solution:

As you can see from the API documentation, Bucket.upload doesn’t take a buffer. It takes a string path to a file. If you have a buffer to upload, you should use File.save() instead.

See also:

  • Uploading a buffer to google cloud storage

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article Prevent unchanged children from re-rendering in React Javascript – Prevent unchanged children from re-rendering in React
Next Article D3 Force Network - How to apply function after simulation not only after drag Javascript – D3 Force Network – How to apply function after simulation not only after drag
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

How to set the dropdown value by clicking on a table row

Javascript – How to set the dropdown value by clicking on a table row

February 11, 2024
Attempting to increase the counter, when the object's tag exist

Javascript – Attempting to increase the counter, when the object’s tag exist

February 11, 2024
Cycle2 JS center active slide

Javascript – Cycle2 JS center active slide

February 10, 2024
Can import all THREE.js post processing modules as ES6 modules except OutputPass

Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass

February 10, 2024
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?