Search code examples
node.jsexpressmulter

How do I get the req.files.path when uploading multiple images with Multer


I would like to get an array of strings and each string is the path of where I have saved the image, all images are being save to a folder.

router.post("/", upload.array("songImage"), (req, res, next) => {
    // console.log(req.files[0].originalname);
    var file = req.files;
    console.log(file.path) // THIS IS WHERE I WANT TO GET THE ARRAY OF FILE PATHS
    const song = new Song({
      _id: new mongoose.Types.ObjectId(),
      name: req.body.name,
      composer: req.body.composer,
      productImage: req.files.path
    });

When I run this get 'undefined'.

Thanks


Solution

  • Short answer

    var paths = req.files.map(file => file.path)
    

    Slightly longer answer

    req.files is an array of objects (files), so you have to use Array.prototype.map() to create a new array that contains only the path of each file.