Reputation: 391
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
Upvotes: 1
Views: 4144
Reputation: 3829
also you can use this method
var path = req.files.objName[0].path
the number 0 differs based on which object you want to access
Upvotes: 0
Reputation: 2161
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.
Upvotes: 4