Still don't know how to make it work on edit/update
Here's my attempt
//route for handling product edit view
router.get('/edit/:id', upload.single('image'), async (request, response)=>{
let product = await Product.findById(request.params.id);
response.render('edit', { product: product });
});
//route for handling product edit
router.put('/:id', upload.single('image'), async (request, response)=>{
request.product = await Product.findByIdAndUpdate(request.params.id);
let product = request.product;
product.name = request.body.name;
product.price = request.body.price;
product.stock = request.body.stock;
product.img = request.file.filename;
try {
product = await product.save();
//redirect to show route
response.redirect(`/products/${product.slug}`);
} catch (error) {
console.log(error);
response.redirect('/products/edit/${product.id}', { product: product });
}
});
but it produces error
(node:10876) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'filename' of undefined
at E:\Program Files\Microsoft VS Code\Projects\Jannah Firdaus\jf-server\routes\products.js:76:32
at processTicksAndRejections (internal/process/task_queues.js:97:5)
Anyone know how to fix this?