Step 3

Let's implement NoteDao.create method:

async create(content, author) {
  const note = await Note.create({ content, author });
  return note;
}

Notice I'm using the Async/Await pattern to deal with asynchronous Note.create operation:

  • The NoteDao.create method signature is modified by placing an async keyword in front of it.
  • The Note.create method is called and the call is preceded with await operator.

Notice I have not included the input validation which we had before. The Note.create method takes care of it (well, actually, Mongoose is taking care of it since when we defined the schema for notes, we specified "content" and "author" as "required" attributes).

If anything goes wrong with creating (and saving) the note, the Note.create will throw an error, and that error will be propagated in our application. So any program that calls NoteDao.create can wrap it inside a try-catch block to deal with potential errors.

Also note, the argument to Note.create is an "object" not the parameters "content" and "author".