Am new in Nodejs
, In my project am trying to upload image in Edit page
.
Here am using two conditions, those are following:
- If user select image file means
- With out selecting
If user select image file new image
upload
and save it in mydatabase.
else save old_image data without
upload file
.
This is my code:
router.post('/edit_coupon/:id', verifyToken, function(req, res, next){
let update_coupon = {};
var file = req.files.image;
if(Object.keys(req.files).length != 0) // if user select file
{
var random = Math.floor(Math.random() * (999999 - 100000 + 1)) + 100000;
const image_name = random+file.name;
file.mv('public/assets/images/coupons/'+image_name, function(err){
if (err)
{
return res.status(500).send(err);
}
});
update_coupon.image = image_name;
}
else
{
update_coupon.image = req.body.old_image; // if user didnot select file
}
// code for update
...
...
...
});
My above code not working when user without select image showing error like
TypeError: Cannot convert undefined or null to object at Function.keys ()