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.createmethod signature is modified by placing anasynckeyword in front of it. - The
Note.createmethod is called and the call is preceded withawaitoperator.
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.createwill throw an error, and that error will be propagated in our application. So any program that callsNoteDao.createcan 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".