exports.product_post = (req, res, next) => {
if(req.file){
// ?! На випадок якщо зображення є
console.log('Спрацював перший випадок');
let product;
return function(){
product = new Product({
_id: new mongoose.Types.ObjectId(),
name:
req.body.name,
price: req.body.price,
productImage: req.file.path
});
}
} else {
// ?! На випадок якщо зображення немає
console.log('Спрацював другий випадок');
let product;
return function(){
product = new Product({
_id: new mongoose.Types.ObjectId(),
name:
req.body.name,
price: req.body.price
});
}
}
product.save()
.then(result => {
console.log(result);
res.status(200).json({
name:
result.name,
price: result.price,
productImage: result.productImage
});
})
.catch(err => {
console.log('Error from post/ : ' + err);
res.status(500).json(err);
})
}