Question: help with error ***MongoServerError: Performing an update on the path '_id' would modify the immutable field '_id'***** This is for an edit button, the link

help with error

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

This is for an edit button, the link works but for some reason when i change the data on the edit page and hit submit the whole website crashes

here is the line of code, let me know if you see any errors i may have missed

">Edit

export function displayEditPage(req, res, next) {

let id = req.params.id;

booksModel.findById(id, function(error, book){

if (error) {

console.error(error);

res.end(error);

}

res.render('index', {title: ' Edit Book', page: 'books/edit', book})

})

}

// POST process the Book Details page and create a new Book - CREATE

export function processEditPage(req, res, next) {

let id = req.params.id

let editBook = booksModel({

_id: req.body.id,

name: req.body.name,

author: req.body.author,

published: req.body.published,

description: req.body.description,

price: req.body.price,

});

booksModel.updateOne({_id: id},editBook,function(error, book){

if (error) {

console.error(error);

res.end(error);

}else{

res.redirect('/books/list');

};

//res.redirect('/books/list');

});

// GET the Book Details page in order to edit an existing Book

router.get('/books/edit/:id', displayEditPage);

// POST - process the information passed from the details form and update the document

router.post('/books/edit/:id', processEditPage);

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!