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

293
Views
How to update file/images with patch api and multer package

As you can see my codes, I created a user model and importing here(inside controller) and also imported a upload middleware from my another directory folder where codes is written for uploading file/images using multer package and everything is ok and perfectly upload the file using post api.

what i am doing here -> created a user schema model, and a middleware for uploading file using multer package and controller for crud operation post,get ,patch and delete, post api is ok its working fine but for patch

what i want to do -> I want to update profile_pic through (req.params.id) when i use patch api in postman to update so its uploaded in upload directory but at the same time i got an error called(Cast to ObjectId failed for value) in the postman and seems not not updated my data

const express = require('express');
const User = require('../models/user.model');
const upload = require('../middlewares/file_upload')
const router = express.Router();


router.post('/',upload.single("profilePic"),async (req, res) => {
    try {
        const users = await User.create({
            first_name:req.body.first_name,
            last_name:req.body.last_name,
            profile_pic:req.file.path,
        })
        return res.status(201).send({users})

    } catch(e) {
        return res.status(500).json({message:e.message,status:"Failed"})
    }
})



`Here below the problem is, Can anyone help me out to this problem`

router.patch('/:id',upload,async (req, res) => {


    try {
       
        const user = await User.findByIdAndUpdate({
            first_name:req.params.id,
            last_name:req.params.id,
            profile_pic:req.file.path,
          
        },{req.body}, { new:true})

        return res.status(201).send({user})

    } catch(e) {
        return res.status(500).json({message:e.message,status:"Failed"})
    }
})

module.exports = router;
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Try this code:

router.patch('/:id',upload.single("profilePic"),async (req, res) => {

try {
   
    const user = await User.findByIdAndUpdate(
       mongoose.Types.ObjectId(req.params.id),
       { profile_pic: req.file.path }
    );
    return res.status(201).send({user})

} catch(e) {
    return res.status(500).json({message:e.message,status:"Failed"})
}

})

The problem is, that you are finding the user by first_name and last_name, which are no ids. You need to find the user by the object id.

about 4 years ago · Juan Pablo Isaza Report
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!