Step 4

Let's implement the "read" operations:

async readAll(author = "") {
  const filter = author ? { author } : {};
  const notes = await Note.find(filter);
  return notes;
}

async read(id) {
  const note = await Note.findById(id);
  return note;
}

Notice the same Async/Await pattern is applied here.

The Note.find method takes an optional parameter, a filter, which can be used to search for notes that match the given attribute values. So, for example if we want to search for all notes written by "Bob" we can do this:

const notes = await Note.find({ author: "Bob" });

If we want to receive all notes, we can call Note.find with no argument

const notes = await Note.find();

Or with an empty filter object

const notes = await Note.find({});

If there are no "notes" in the database, or there is no match for the filter we have provided, the Note.find method returns an empty array. On the other hand, if there is no match for the ID we have provided to Note.findById, it will return null. These behaviors are consistent with how we have designed and implement our NoteDao "read" operations.