while looking for solutions on this topic, i have not been able to find anything directly relating to mongodb, only in relation to multer itself. so, for one of my api routes i wish to have an image upload using multer which has multiple fields, rather than uploading them as an array.
the main function of this api is to grab the image, save the file to the local FS, and then save the filename in mongodb, however i’m unsure on how to handle this filename.
when using code like this, it only returns [Black.jpg] rather than the filename, additionally the image does show up in the FS with the proper name
if possible, i would like to stay away from using .any(), but all of the solutions i have found i have been unable to implement
thanks :))
const storage = multer.diskStorage({
destination: (req, res, cb) => {
cb(null, dir)
},
filename: (req, file, cb) => {
cb(null, Date.now() + file.originalname)
}
})
const upload = multer({
storage: storage
})
router.post('/', upload.any(), async (req, res) => {
const business = new Business({
name: req.body.name,
type: req.body.type,
phone: req.body.phone,
address: req.body.address,
coverimg: req.file!==undefined ? req.file.filename : 'Black.jpg',
mainimg: req.file!==undefined ? req.file.filename : 'Black.jpg'
})
try {
const newBusiness = await business.save()
res.status(201).json(newBusiness)
} catch (err) {
res.status(400).json({ message: err.message });
}
})
2
Answers
yay, i found out that while using upload.fields and accessing it via an array i can finally get the value, here's how :D
cool :p