Handling the files in Express using Multer

I am trying to add the product details along with the product Image to the MySql database using NodeJs/Express and postman, but I am getting the issue of filename undefined when i try to make a request to post the product.
I am getting the images that i uploaded from the postman in my code but i cannot send the request of the product.

const db = require("../Database/database");
const multer = require("multer");
const path = require("path");

// *******HANDLING THE FILES********
const storage = multer.diskStorage({
  destination: (req, file, cb) => {
    cb(null, path.join(__dirname, "../assets"));
  },
  filename: (req, file, cb) => {
    const extension = path.extname(file.originalname);
    cb(null, `${new Date().toISOString().replace(/:/g, "-")}.${extension}`);
  },
});

const upload = multer({
  storage: storage,
  fileFilter: (req, file, cb) => {
    if (!file.originalname.match(/\.(jpg|jpeg|png|gif)$/)) {
      return cb(new Error("Only image files are allowed"));
    }
    cb(null, true);
  },
  limits: {
    fileSize: 1024 * 1024 * 5, // 5MB file size limit
  },
}).single("prodImage");



Here as well.
// ***********ADD A PRODUCT**********
const addProduct = async (req, res) => {
  try {
    // Validate that all required fields are provided
    const { prodName, prodAddress, prodDescription, prodPrice } = req.body;
    if (!prodName || !prodAddress || !prodDescription || !prodPrice) {
      return res
        .status(400)
        .json({ message: "Please provide all product details" });
    }

    // Access uploaded file information from req.file
    const prodImage = path.join("../assets", req.file.filename);
    console.log(prodImage);

    // Proceed with the database query using prodImage
    const [result] = await db.query(
      "INSERT INTO product(prodName, prodImage, prodAddress, prodDescription, prodPrice) VALUES (?, ?, ?, ?, ?)",
      [prodName, prodImage, prodAddress, prodDescription, prodPrice]
    );

    res.status(200).json({ message: "A product has been added successfully" });
  } catch (error) {
    console.error(error);
    res.status(500).json({ message: "Internal server error" });
  }
};

My GitHub repo : KiitEats/backend at master · Iyabivuz-e/KiitEats · GitHub

Hello, So your doing a backend project but every thing isnt going as expected, what Ide are you using? Have you gone through and ran the fix commands for npm and taken feed back etc…

Helpful Information:

Generative AI is experimental. Learn more

Here are some steps you can follow to add product details and images to a MySQL database using Node.js, Express, and Postman:

    1. Install the required dependencies.

You will need to install the following packages:

  • Express
  • MySQL
  • Multer (for handling file uploads)
  • Body-parser (for parsing JSON and url-encoded data)
    1. Create a new Express application.

Once you have installed the required dependencies, you can create a new Express application by running the following command:

npm init -y

This will create a new file called package.json which will contain the dependencies for your application.

    1. Configure the MySQL database.

You will need to configure the MySQL database to store the product details and images. You can do this by creating a new database and user, and then granting the user the necessary permissions.

    1. Create a model for the product data.

You will need to create a model for the product data which will define the structure of the data that will be stored in the database. The model should include fields for the product name, description, price, and image.

    1. Create a controller for handling product requests.

You will need to create a controller for handling product requests. The controller will be responsible for adding, updating, and deleting product data from the database.

    1. Create routes for the product endpoints.

You will need to create routes for the product endpoints. The routes will define the URLs that can be used to access the product data.

    1. Start the Express application.

Once you have created the Express application, you can start it by running the following command:

node server.js

  1. Use Postman to send requests to the product endpoints.: You can use Postman to send requests to the product endpoints to add, update, and delete product data.

Here is an example of a request to add a new product:

POST http://localhost:3000/products Content-Type: application/json { “name”: “Product Name”, “description”: “Product Description”, “price”: 100, “image”: “image.png” }

If you are getting the error filename undefined when you try to make a request to post the product, it is likely that you are not passing the image file to the server correctly. Make sure that you are setting the Content-Type header to multipart/form-data and that you are including the image file in the request body.

You can also check the Express server logs to see if there are any errors being logged. The logs may provide more information about the cause of the error.

I ran this in pythontutor it threw an error for the uncommented plain english, but when removed the next line was unexpected token, my question was the format your using for an arrow function correct?

I followed all the processes but still not working. And I am using postman to send images as well but still not working.