(node:17348) UnhandledPromiseRejectionWarning: MongoError: Performing an update on the path '_id' would modify the immutable field '_id'

Alish Madhukar

Getting this error on trying to update....mongoose is throwing this error on using the code below....have tried using const project = {...} instead of new Product({...}) but no luck.

router.put("/:id", multer({storage: storage}).single("image") , (req, res, next) => {
    let imagePath = req.body.imagePath;
    if (req.file) {
        const url = req.protocol + "://" + req.get("host");
        imagePath = url + "/images/" + req.file.filename
    }
    const product = new Product(
        {
            _id: req.body.id,
            title: req.body.title,
            cost: req.body.cost,
            description: req.body.description,
            imagePath: imagePath
    
        }
    ) ;
    console.log(product)
    Product.updateOne({_id: req.params.id}, product)
    .then(result => {
        console.log(result);
        res.status(200).json({ message: "Update Successful !!"});
    });
});
Avani Khabiya

You need to remove the key _id from the object that is being used for updating the document.

const product = new Product(
    {
        title: req.body.title,
        cost: req.body.cost,
        description: req.body.description,
        imagePath: imagePath
    }
) ;

This will solve your problem.

Thing is, while doing an update on the document whose _id matches with the one you passed in your code,

    Product.updateOne({_id: req.params.id}, product)

you are trying to update the value of _id field as well. _id by default is immutable which means it can not be updated, as it represents the uniqueness of the document.

Edit: If you need to update the _id of the document as well, then you can take a reference from here to do this. Though the _id can not be updated directly, but there is a workaround for doing that.

How to update the _id of one MongoDB Document?

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Performing an update on the path '_id' would modify the immutable field '_id'

Mongoose / MongoDB Performing an update on the path \'_id\' would modify the immutable field \'_id\'

MeanStack: conflict on editing feature'_id' would modify the immutable field '_id'

(node:32032) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): MongoError: Authentication failed

UnhandledPromiseRejectionWarning: MongoError: Cannot create field 'semesters' in element

node.js mongodb .. the (immutable) field '_id' was found to have been altered

MongoWriteConcernException. the (immutable) field '_id' was found to have been altered to _id

UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1) in Node.JS

(node:6316) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'id' of undefined

How to solve performing update on immutable fields in mongodb

Get Path to node with specific id

jsonpatch path to update array object by object ID

MongoDB - Update every "_id" field on a collection with an iteration

EF: Best/Quickest way to update field by Id

UnhandledPromiseRejectionWarning: CastError: Cast to ObjectId failed for value "undefined" at path "_id" for model "User"

Update one document by _id (Invalid BSON field name _id)

Create browse path from node ID

Modify immutable scala class field via reflection

How to modify cloned field form value and id inside table row?

(node:8592) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 3): MongoParseError: Invalid connection string

Firebase getting ID for specific node to delete/update

How can i fix (node:14352) UnhandledPromiseRejectionWarning: MongoError: E11000 duplicate key error collection: error?

About the changing id of an immutable string

UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'id' of undefined

Selenium not performing button click using ID - Java

Get the ID after performing an insert statement in laravel

Update SQL query to change a field based on ID in same table

Update Mysql column field from a table based on user id

Rails 5: Insert/Update only if there's no filled specific field for that id