-
Notifications
You must be signed in to change notification settings - Fork 0
/
Update.js
27 lines (27 loc) · 814 Bytes
/
Update.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
const router = require('express').Router();
const product = require('../../models/product');
// Updating data
router.put('/product/:id', async (req, res) => {
try {
const data = await product.findByIdAndUpdate(req.params.id, req.body, {
new: true,
useFindAndModify: false,
runValidators: true
});
if (!data) {
return res.status(404).json({
success: false,
message: "Product not found."
});
}
res.status(200).json({
success: true,
message: "Product modified successfully.",
response: data
});
} catch (err) {
console.error('Error updating product:', err.message);
res.status(500).json({ success: false, message: 'Internal server error' });
}
});
module.exports= router;