0

I'm trying to ulpoad an image with multer and postman and the upload is working. I just got one (big) problem : the name of the photo is always undefined.jpg when I want it to be req.body.name + "jpg". Is there a problem with the req.body ? Here is my code: In router.js:

const router = require("express").Router();
const userController = require("../controllers/user.controller");
const authController = require("../controllers/auth.controller");
const uploadController = require("../controllers/upload.controller")
const multer = require("multer")

// auth
router.post("/register", authController.signUp);
router.post("/login", authController.signIn);
router.get("/logout", authController.logout);
//user display block
router.get("/", userController.getAllUsers);
router.get("/:id", userController.userInfo);
router.put("/:id", userController.updateUser);
router.delete("/:id", userController.deleteUser);
router.patch("/follow/:id", userController.follow);
router.patch("/unfollow/:id", userController.unfollow);
// upload
router.post("/upload", uploadController.upload)
module.exports = router;

in upload.controller.js:

const uploadFile = require("../middleware/upload.middleware");
module.exports.upload = async (req, res) => {
  try {
    await uploadFile(req, res);
    if (req.file == undefined) {
      return res.status(400).send({ message: "Please upload a file!" });
    }
    res.status(200).send({
      message: "Uploaded the file successfully: " + req.body.name + ".jpg",
    });
  } catch (err) {
    res.status(500).send({
      message: `Could not upload the file: ${req.body.name + ".jpg"}. ${err}`,
    });
  }
};

And in upload.middleware.js:

const util = require("util");
const multer = require("multer");
const maxSize = 2 * 1024 * 1024;
let storage = multer.diskStorage({
  destination: (req, file, cb) => {
    cb(null, __dirname + "/../client/public/uploads/profil");
  },
  filename: (req, file, cb) => {
    cb(null, req.body.name + ".jpg");
  },
});
let uploadFile = multer({
  storage: storage,
  limits: { fileSize: maxSize },
}).single("file");
let uploadFileMiddleware = util.promisify(uploadFile);
module.exports = uploadFileMiddleware;

Do you have any idea what's happening ? Thanks for your help Alx

Christian Baumann
  • 3,188
  • 3
  • 20
  • 37

2 Answers2

0

try to use app.use(express.json()) in index.js

var express = require('express')
var app = express()
var port = process.env.PORT || 8000
var cors = require('cors');
var routes = require('./src/routes');

app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cors());
app.use(routes)

app.listen(port, () => console.log(`localhost:${port}));
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
Guest27
  • 1
  • 1
  • It’s still not working. I think it’s not a problem with the express but just that the req is not defined when creating the variable « fileName » cause the await is after – Alexandre Calcio Gaudino Aug 20 '22 at 12:17
0

basically in your upload.controller.js The uploaded file wont be available in req.body. You have to use req.file in your controller.

const uploadFile = require("../middleware/upload.middleware");
module.exports.upload = async (req, res) => {
  try {
    await uploadFile(req, res);
    if (req.file == undefined) {
      return res.status(400).send({ message: "Please upload a file!" });
    }
    res.status(200).send({
      message: "Uploaded the file successfully: " + req.file.filename 
    });
  } catch (err) {
    res.status(500).send({
      message: `Could not upload the file: ${req.file.filename}. ${err}`,
    });
  }
};
Alpha
  • 1,413
  • 3
  • 9
  • 23