Step 1

We are starting where we left off with refactoring YouNote API.

Install mongoose:

npm install mongoose

As the first step, we will update our model for "note"; open model.Note.js file which currently contains the following:

class Note {
  constructor(content, author) {
    this.content = content;
    this.author = author;
  }
}

module.exports = Note;

Update the source code:

const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const NoteSchema = new Schema({
  content: { type: String, required: true },
  author: { type: String, required: true },
});

const Note = mongoose.model("Note", NoteSchema);

module.exports = Note;

As it can be seen, we are making a schema for notes document and link that schema to a mongoose.model called Note. Note the attribute required: true in the NoteSchema; this attribute will help us with input validation.