Created
June 1, 2016 04:57
-
-
Save l02162010/ccaca3c8272e414ffb18b901e52dfb6c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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