Req.files is undefined with multer

hello, after I managed to save the image in the uploads folder. now I can’t save it in the donation database with mongodb
the console.log gives me image2Upload: ‘http: // localhost: 3000 / public / uploads / undefined’,

folder router

const multer = require('multer')

var Storage = multer.diskStorage({
    destination: function(req, file, callback) {
        callback(null, "./public/uploads");
    },
    filename: function(req, file, callback) {
        callback(null,  file.originalname);
    }
});

const upload = multer({
  storage: Storage
  }).fields([{name: "image1Upload", maxCount: 1}, {name: "image2Upload", maxCount: 1}]);
 
 router.post('/ajouter-annonce', upload, AnnonceController.annoncePost)

folder Controller

const annoncePost = (req, res, next) =>{

 const url = req.protocol + '://' + req.get('host');
 const {titreBien, description, email, address, Categories, EtatBien } = req.body
 
 const saveAnnonce = new Annonce({
   titreBien,
   description,
   email,
   address,
   Categories,
   EtatBien,
   image2Upload: url + '/public/uploads/' +  req.files['image2Upload'][1]
 })

//   url + '/public/uploads/' + req.file.filename

 console.log(saveAnnonce);
 next()

}

result console.log(
{
  _id: 60185a53e2cdee45201b9334,
  titreBien: 'deballage',
  description: 'test',
  email: 'ahmadanote26@gmail.com',
  address: '24 Rue du maréchal juin',
  Categories: 'Habillement',
  EtatBien: 'Etat Neuf',
  image2Upload: 'http://localhost:3000/public/uploads/undefined',
  date: 2021-02-01T19:45:23.356Z
}


)

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.