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. If id does not match an existing note, the findByIdAndUpdate will return null.
  • 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: true changes the default behavior of findByIdAndUpdate to return the updated note (instead of the original note).
    • runValidators: true changes the default behavior of findByIdAndUpdate to force running validators on new attributes. If validation fails, the findByIdAndUpdate operation throws an error.

We are no done with operations of NoteDao. Next we will update the "notes" Router object.