Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

243
Views
How can I update image from nodejs with multer

Worked for posting method

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?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!