1// Update a note identified by the noteId in the request
2exports.update = (req, res) => {
3 // Validate Request
4 if(!req.body.content) {
5 return res.status(400).send({
6 message: "Note content can not be empty"
7 });
8 }
9
10 // Find note and update it with the request body
11 Note.findByIdAndUpdate(req.params.noteId, {
12 title: req.body.title || "Untitled Note",
13 content: req.body.content
14 }, {new: true})
15 .then(note => {
16 if(!note) {
17 return res.status(404).send({
18 message: "Note not found with id " + req.params.noteId
19 });
20 }
21 res.send(note);
22 }).catch(err => {
23 if(err.kind === 'ObjectId') {
24 return res.status(404).send({
25 message: "Note not found with id " + req.params.noteId
26 });
27 }
28 return res.status(500).send({
29 message: "Error updating note with id " + req.params.noteId
30 });
31 });
32};
33