The Notes API: Updating and Deleting
Updating and deleting are the two routes that change notes after they exist. PATCH changes a note, and DELETE removes one.
Updating a note
The route applies the change and sends the result.
app.patch('/api/notes/:id', async (req, res) => {
const note = await Note.findByIdAndUpdate(req.params.id, req.body, { new: true });
res.json({ title: note.title, done: note.done });
});Deleting a note
The route removes the note and sends it back.
app.delete('/api/notes/:id', async (req, res) => {
const note = await Note.findByIdAndDelete(req.params.id);
res.json({ title: note.title, done: note.done });
});The list after the change
Only two notes remain.
curl http://localhost:3001/api/notesExample
import express from 'express';
import mongoose from 'mongoose';
await mongoose.connect('mongodb://localhost:27017/notes');
const noteSchema = new mongoose.Schema({ title: String, done: Boolean });
const Note = mongoose.model('Note', noteSchema);
await Note.create({ title: 'Buy milk', done: false });
await Note.create({ title: 'Ship MERN', done: true });
await Note.create({ title: 'Read a book', done: false });
const app = express();
app.use(express.json());
app.patch('/api/notes/:id', async (req, res) => {
const note = await Note.findByIdAndUpdate(req.params.id, req.body, { new: true });
res.json({ title: note.title, done: note.done });
});
app.delete('/api/notes/:id', async (req, res) => {
const note = await Note.findByIdAndDelete(req.params.id);
res.json({ title: note.title, done: note.done });
});
app.get('/api/notes', async (req, res) => {
const notes = await Note.find().sort({ title: 1 });
res.json(notes.map((n) => ({ title: n.title, done: n.done })));
});
const server = app.listen(0);
const port = server.address().port;
const base = `http://localhost:${port}`;
const milk = await Note.findOne({ title: 'Buy milk' });
const updated = await fetch(`${base}/api/notes/${milk._id}`, {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ done: true }),
});
console.log(await updated.text());
const book = await Note.findOne({ title: 'Read a book' });
const removed = await fetch(`${base}/api/notes/${book._id}`, { method: 'DELETE' });
console.log(await removed.text());
const list = await fetch(`${base}/api/notes`);
console.log(await list.text());
server.close();
await mongoose.disconnect();Key points
- PATCH changes some fields of a note and leaves the rest alone.
- findByIdAndUpdate finds the note and applies the change.
- The option { new: true } returns the changed note, not the old one.
- DELETE removes a note and sends the removed note back.
- findByIdAndDelete finds the note and removes it in one step.
In this example, the script connects to the notes database and saves the three notes. It builds an Express app with a PATCH route, a DELETE route, and a list route. The PATCH route marks Buy milk as done and sends back the changed note. The DELETE route removes Read a book and sends it back. The script then asks for the list, which holds only the two notes that remain.
Note
A second list shows only the notes that remain.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
What does PATCH change in a note?