Estoy haciendo una solicitud de publicación en el punto final de la API http://localhost:5000/api/notes/updatenote:id donde id es una id of a particular user . La API simplemente actualiza las notas asociadas con el usuario, donde las notes son en sí mismas una colección de MongoDB con su propia identificación. Estoy tratando de hacer la solicitud PUT pero recibo este error. Había hecho mucho commit para una fácil comprensión del código, pero en caso de duda, puedes pedirme más información.
Error
CastError: Cast to ObjectId failed for value ":61473ebd4ce15cca37954d28" (type string) at path "_id" for model "notes"codigo para poner
//ROUTE 3 :Update notes of a particular user PUT:/api/notes/updatenote:id router.put("/updatenote:id", fetchUser, async (req, res) => { console.log("put request starts"); const { updatedTitle, updatedDescription, updatedTag } = req.body; //console.log(req.body); //Create a newNote object let newNote = {}; if (updatedTitle) { newNote.title = updatedTitle; } if (updatedDescription) { newNote.description = updatedDescription; } if (updatedTag) { newNote.tag = updatedTag; } console.log("param val - "+req.params.id) //finding the note to be updated and update it let noteToBeUpdated = await Notes.findById(req.params.id); //id in the parameter console.log(noteToBeUpdated); if (!noteToBeUpdated) { return res.status(404).send("Not found"); } //req.user.id == person ki id who is updating and notetobeupdated.user = jiska note update ho rha uski id if (noteToBeUpdated.user.toString() !== req.user.id) { return res.status(401).send("Unauthorised User"); } noteToBeUpdated = await Notes.findByIdAndUpdate( req.params.id, { $set: newNote }, { new: true } ); res.json({ noteToBeUpdated }); });cuerpo de la solicitud
{ "title":"First song (debut)", "description":"First Debut song in launched in 2015.", "tag":"music" }colección de notas
[ { "_id": "61473ebd4ce15cca37954d28", "user": "61432ac5225cd230999f178d", "title": "First song", "description": "First song in English language worldwide.", "tag": "science", "date": "2021-09-19T13:44:29.386Z", "__v": 0 } ]su cadena _id contiene : y mangosta podría convertir esta cadena en un cambio objetado router.put("/updatenote:id" a router.put("/updatenote/:id"