Answer the question
In order to leave comments, you need to log in
How to implement the Update function in a MERN application?
Good day.
I'm trying to figure out the MERN stack, using the example of an application found on github
Based on this repository, I already sketched my first application, with some additions and changes, and when I decided to add the Update function, I got stuck ... (get, add, delete implemented)
Answer the question
In order to leave comments, you need to log in
from react in the body of the request you send the _id of the item that you want to update
then in the express route, for example, via
https://mongoosejs.com/docs/api.html#model_Model.f...
Item.findOneAndUpdate(
{_id: req.params.id }, // ищем айтем по id
{ $set: { name: req.params.name } } // меняем поле name на полученное в теле запроса из реакта
).then( () => {
// всё прошло удачно
res.json({ success: true })
} )
.catch( err => {
// всё прошло крайне неудачно
res.json({ success: false })
} )
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question