'Insert multiple image & single image in single api in node using multer
I write API for image upload with multiple image and single image concept in one api call.
File is uploading in corresponding folder but filename not inserted into the database.
I want to insert logo (single image) into the business table & gallery (multiple image) in another table with multiple rows.
Image uploaded in folder also but filename is not getting
This is my API:
const files=multer.diskStorage({
destination:(req,file,cb)=>{
cb(null,"./assets/uploads/business");
},
filename: (req,file,cb)=>
{
cb(null,"st-"+Date.now()+ file.originalname);
}
});
const uploadimg=multer({
storage:files
});
const cpUpload = uploadimg.fields([{ name: 'logo' }, { name: 'galleries', maxCount: 4 }])
app.post('/addBusiness',cpUpload,async(req,res)=>{
const data=
{
logo:req.files.filename,
businessgallery:req.files['galleries']
}
models.business.create(data, {
include: [{
model: models.businessgallery
}]
}).then(result=>{
res.status(201).json({
code:"1",
Message:"Business created successfully",
Data:result
});
})
.catch(error=>{
res.status(500).json({
Message:"Something went wrong",
error:error
});
})
});
I put hasmany belongs to concept
business.js
models.businessgallery.belongsTo(models.business);
businessgallery.js
models.business.hasMany(models.businessgallery);
how to get corresponding name to corresponding table
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
