I am using express-fileupload to upload the images. The images are saved in my local directory. I want to insert the name of the file to the mongodb if possible. Finally I want the image to be displayed in my frontend.
    function insertRecord(req,res){
    if(req.files){
    const file=req.files.filename
    filename=file.name
    file.mv("./upload"+filename,function(err){
        if(err)
        console.log(err)
    })
}
    const user=new User()
    user.name=req.body.name
    user.address=req.body.address
    user.email=req.body.email
    user.mobile=req.body.mobile
    user.filename=req.body.filename
    user.save((err,docs)=>{
        if(!err){
        res.redirect('/user/list')
        }
        else {
            if (err.name == 'ValidationError') {
                handleValidationError(err, req.body);
                res.render("./users/addOrEdit", {
                    viewTitle: "Insert User",
                    user: req.body
                });
            }
            else
                console.log('Error during record insertion : ' + err);
        }
    });
}
I am not sure whether the way to insert the name of the file to the mongodb is correct or not. Anyway, that is optional but I am not understanding how can I display the uploaded images which are present in the local directory. I tried to save the image as base64 but the record is not saved to the database now.
    var storage = multer.diskStorage({
    destination: function (req, file, cb) {
      cb(null, 'uploads/')
    },
    filename: function (req, file, cb) {
      cb(null, file.fieldname + '-' + Date.now())
    }
  })
  var upload = multer({ storage: storage })
router.post('/',upload.single('myImage'),function(req,res){
    if (req.body._id == '')
        insertRecord(req, res);
        else
        updateRecord(req, res);
})
function insertRecord(req,res){
var img = fs.readFileSync(req.file.path);
var encode_image = img.toString('base64');  
 var finalImg = {
      contentType: req.file.mimetype,
      image:  new Buffer(encode_image, 'base64')
   };
    const user=new User()
    user.name=req.body.name
    user.address=req.body.address
    user.email=req.body.email
    user.mobile=req.body.mobile
    user.save(finalImg,(err,docs)=>{
        if(!err){
        res.redirect('/user/list')
        }
        else {
            if (err.name == 'ValidationError') {
                handleValidationError(err, req.body);
                res.render("./users/addOrEdit", {
                    viewTitle: "Insert User",
                    user: req.body
                });
            }
            else
                console.log('Error during record insertion : ' + err);
        }
    });
}
 
    