Store multiple images simultaneously in mongodb using node js

Viewed 543

I have a form look like below image. enter image description here

In this "Add Row" button will allow user to add one more row of the same fields.

I want to store the informations in MongoDB atlas using node js.

    var mongoose = require('mongoose');
  
var imageSchema = new mongoose.Schema({
    name: String,
    desc: String,
    img:
    {
        data: Buffer,
        contentType: String
    }
});
  
//Image is a model which has a schema imageSchema
  
module.exports = new mongoose.model('Image', imageSchema);

I found this Schema code from geeks for geeks to store the image. But I didn't get any resource that will help me to add multiple images when "Add Row" button will hit. And to upload image to MongoDB I found below code. I also want to have this code as per the values in UI. I mean if there are 2 rows then I want my object to have data of 2 rows specially with 2 images.

app.post('/', upload.single('image'), (req, res, next) => {
  
    var obj = {
        name: req.body.name,
        desc: req.body.desc,
        img: {
            data: fs.readFileSync(path.join(__dirname + '/uploads/' + req.file.filename)),
            contentType: 'image/png'
        }
    }
    imgModel.create(obj, (err, item) => {
        if (err) {
            console.log(err);
        }
        else {
            // item.save();
            res.redirect('/');
        }
    });
});

It would be better if anyone can re-write above 2 scripts with my requirements. All I want is design schema according to the rows and in second script I want obj according to the rows in UI.

Thanks.

0 Answers
Related