Editing an existing note – update

Now that we've looked at the create and read operations, let's look at how to update or edit a note. 

To routes/notes.js, add this router function:

// Edit note (update)router.get('/edit', async (req, res, next) => {    var note = await notes.read(req.query.key);    res.render('noteedit', {        title: note ? ("Edit " + note.title) : "Add a Note",        docreate: false,        notekey: req.query.key, note: note    });});

We're reusing the noteedit.ejs template, because it can be used for both create and update/edit operations. Notice that we pass false for docreate, informing the template that it is to be used for editing.

In this case, we first retrieve the note object and then pass it through to the template. This way, the template ...

Get Node.js Web Development - Fourth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.