Step 5
Here is the NoteDao.delete operation:
async delete(id) {
const note = await Note.findByIdAndDelete(id);
return note;
}
The Note.findByIdAndDelete will delete and return the deleted note if id exist in the database. If the id does not exist, it will return null.
Here is the NoteDao.update operation:
async update(id, content, author) {
const note = await Note.findByIdAndUpdate(
id,
{ content, author },
{ new: true, runValidators: true }
);
return note;
}
Notice three parameters are provided to Note.findByIdAndUpdate:
id: the ID of a note in your database to be updated. Ifiddoes not match an existing note, thefindByIdAndUpdatewill returnnull.- An object containing the new attributes (and their values) which are to replace the existing attribute values of the note to be updated.
- An object of parameters:
new: truechanges the default behavior offindByIdAndUpdateto return the updated note (instead of the original note).runValidators: truechanges the default behavior offindByIdAndUpdateto force running validators on new attributes. If validation fails, thefindByIdAndUpdateoperation throws an error.
We are no done with operations of NoteDao. Next we will update the "notes" Router object.