Skip to content
Advertisement

Multer – Cannot read property ‘buffer’ of undefined

I have a problem uploading an image file to my server, I watched some tutorials on YouTube about multer and I do exactly the same thing that is done in the tutorial and for whatever reason, I get an error: (“Cannot read property ‘buffer’ of undefined”), and req.file is also undefined. I googled for the error and found some people having the same issue and I tried to solve it like them, but it didn’t work for me.

COMPONENT Data App

newPostByUser(token, formData) {
    return (async () =>
      await call(`${this.url}/lost`, {
        method: "POST",
        headers: {
          Authorization: `Bearer ${token}`,
          "Content-Type": "multipart/form-data",
        },
        body: formData,
        timeout: this.timeout,
      }))();
  },

COMPONENT ImageParse

const path = require("path");
const multer = require("multer");
const storage = multer.memoryStorage();
const limits = { fileSize: 2000000 };
const upload = multer({
  storage,
  limits,
  fileFilter(req, file, cb) {
    var filetypes = /jpeg|jpg|png|gif/;
    const mimetype = filetypes.test(file.mimetype);
    const extname = filetypes.test(
      path.extname(file.originalname).toLocaleLowerCase()
    );
    if (mimetype && extname) {
      return cb(null, true);
    }
    cb(
      "Error: File upload only supports the following filetypes - " + filetypes
    );
  },
}).single("image");

function imageParse(req, res, next) {
  upload(req, res, function (err) {
    if (err) {
      return res.status(422).json({ error: err.message });
    }
    next();
  });
}

module.exports = imageParse;

COMPONENT Route

require("dotenv").config();
const express = require("express");
const logic = require("../logic");
const auth = require("./auth");
const imageParse = require("../utils/imageParse");
const literals = require("../i18n/literals");

const router = express.Router();

const { research_newPost_messageConfirm } = literals;

router.post("/lost", auth, imageParse, (req, res) => {
  console.log(req.buffer);
  const {
    file: { buffer },
    body: { userId, category, name, age, city, description, lang },
  } = req;
(async () => {
    try {
      const user = await logic.newResearch(
        buffer,
        userId,
        category,
        name,
        age,
        city,
        description,
        lang
      );
      res.status(201).json({ message: research_newPost_messageConfirm[lang] });
    } catch ({ message }) {
      res.status(400).json({ error: message });
    }
  })();
});

module.exports = router;

COMPONENT Logic api

const streamifier = require("streamifier");
const cloudinary = require("cloudinary").v2;
const { Research } = require("../models");
const {
  CLOUDINARY_API_KEY,
  CLOUDINARY_SECRET_KEY,
  CLOUDINARY_NAME,
} = require("../utils/config");
const { logic_newPost_messageError} = require("../i18n/literals");

const logic = {
newResearch(buffer, userId, category, name, age, city, description, lang) {
    try {
      return (async () => {
        cloudinary.config({
          cloud_name: CLOUDINARY_NAME,
          api_key: CLOUDINARY_API_KEY,
          api_secret: CLOUDINARY_SECRET_KEY,
        });

        const image = await new Promise((resolve, reject) => {
          const uploadStream = cloudinary.uploader.upload_stream(
            (err, image) => {
              if (err) throw new LogicError("Image could not be uploaded");
              resolve(image);
            }
          );
          streamifier.createReadStream(buffer).pipe(uploadStream);
        });
        const newPost = await Research.create({
          owner: userId,
          category,
          name,
          age,
          city,
          description,
          image: image.secure_url,
        });

        if (!newPost) throw new Error(`${logic_newPost_messageError[lang]}`);
        console.log("api", newPost);
        return newPost;
      })();
    } catch (error) {
      console.log(error);
    }
  },
};

module.exports = logic;

Advertisement

Answer

it is not req.buffer

it is req.file.buffer

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement