I want to upload images from several fields of the same collection. I am able to upload using one field.
route : service.js
router.post('/register', upload.array('daycare.DCImage',10),(req, res) => {
var paths = req.files.map(file => file.path)
const newService = new Services(req.body);
newService.daycare.DCImage = paths
newService
.save(newService)
.then(data => {
res.send(data);
})
.catch(err => {
res.status(500).send({
message:
err.message || "Some error occurred while creating the Service."
});
});
})
Like daycare.DCImage i want to make grooming.groomingImage, trainer.TrainingImage, dogwalker.DWImage, breeding.breedingImages fields also to facilitate image uploading.
This code currently works perfectly, How can I alter the code to get the desired functions?
I tried including extra upload.array() like
router.post('/register', upload.array('daycare.DCImage',10), upload.array('grooming.GroomingImage',10 ), upload.array('dogwalker.DWImage', 10) , upload.array('trainer.TrainingImage', 10 ), upload.array('breeding.breedingImage', 10) ,(req, res) )
and also other fields in the same upload.array()
router.post('/register', upload.array('daycare.DCImage', 'grooming.GroomingImage', 'trainer.TrainingImage' , 'breeding.breedingImage', 10),(req, res) )
But it did not work.
This is the model service.js
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const User = require('./User');
const ServiceSchema = new Schema({
daycare : {
description : {type : String},
DCImage : [{type : String}],
additionalServices : {type : String},
},
grooming : {
description : {type : String},
GroomingImage : [{type : String}],
additionalServices : {type : String},
},
dogwalker : {
description : {type : String},
DWImage : [{type : String}],
additionalServices : {type : String},
},
trainer : {
description : {type : String},
TrainingImage : [{type : String}],
price : {
daily : {type : String},
weekly : {type : String},
monthly : {type : String}
},
additionalServices : {type : String},
},
breeding : {
breed : {type : String},
age : {type : String},
breedingImages:[{type:String}],
},
});
module.exports = Services = mongoose.model('Services', ServiceSchema);
Use multer upload.fields like this,
router.post('/register', upload.fields([
{ name: "field_name", maxCount: 1 },
{ name: "field_name", maxCount: 1 },
{ name: "field_name", maxCount: 1 },
{ name: "field_name", maxCount: 1 }
])
,(req, res) );
and then upload function like this,
const upload = multer({
storage: 'path-to-storage',
limits: { fileSize: 100000 },
key: function (req, file, cb) {
cb(null, file.originalname);
},
});