Skip to content

Instantly share code, notes, and snippets.

@l02162010
Created June 1, 2016 04:57
Show Gist options
  • Save l02162010/ccaca3c8272e414ffb18b901e52dfb6c to your computer and use it in GitHub Desktop.
Save l02162010/ccaca3c8272e414ffb18b901e52dfb6c to your computer and use it in GitHub Desktop.
router.post('/update/:id', upload.single('photo'), function (req, res, next) {
var id = req.params.id;
var file = req.file;
var data = req.body;
data.price = {
small: data.priceSmall,
big: data.priceBig
};
if(file && !checkFiletype(file)) {
return res.error('mimetype is not allow', routeRoot);
}
models.Item.findById(id).exec().then(function(item) {
item = _.extend(item, data);
return item.save();
}).then(function(item) {
if(!file) {
return res.redirect(routeRoot);
}
fs.rename(file.path, path.join(photoDir, file.filename), function(err) {
if(err) return next(err);
item.photo = file.filename;
item.save().then(function() {
res.redirect(routeRoot);
});
});
}).catch(next);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment