0

I want to send delete request using postman. But I am getting this error message

{
    "status": "fail",
    "message": "Cast to ObjectId failed for value \"feedback\" at path \"_id\" for model \"User\"",
    "error": {
        "stringValue": "\"feedback\"",
        "kind": "ObjectId",
        "value": "feedback",
        "path": "_id",
        "reason": {},
        "message": "Cast to ObjectId failed for value \"feedback\" at path \"_id\" for model \"User\"",
        "name": "CastError",
        "statusCode": 500,
        "status": "fail"
    }
}

here is my delete request

exports.deleteFeedback = catchAsynsc( 
    async (req, res, next) => {
        console.log(req.params);
        const deletefeedback = await feedback.findByIdAndRemove(req.params.id);
        
        res.status(200).json({
            status: 'success',
            data: deletefeedback
        })
    }
);

Please help me to find solution!!

  • Does this answer your question? [What's Mongoose error Cast to ObjectId failed for value XXX at path "\_id"?](https://stackoverflow.com/questions/14940660/whats-mongoose-error-cast-to-objectid-failed-for-value-xxx-at-path-id) – Prathamesh More Sep 30 '20 at 13:28

1 Answers1

0

You need to pass 'req.params.id' as 'ObjectId'

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId;

var objId = new ObjectId(request.params.Id);
cyrilantony
  • 274
  • 3
  • 14