From 242157ae8449d82dc67b972c7798d7887b907437 Mon Sep 17 00:00:00 2001 From: tactonbishop Date: Tue, 30 Aug 2022 15:00:04 +0100 Subject: [PATCH] Last Sync: 2022-08-30 15:00:04 --- .../REST_APIs/5__Integrating_the_database.md | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/Programming_Languages/NodeJS/REST_APIs/5__Integrating_the_database.md b/Programming_Languages/NodeJS/REST_APIs/5__Integrating_the_database.md index 27cecd2..13160f8 100644 --- a/Programming_Languages/NodeJS/REST_APIs/5__Integrating_the_database.md +++ b/Programming_Languages/NodeJS/REST_APIs/5__Integrating_the_database.md @@ -80,7 +80,7 @@ const courseSchema = new mongoose.Schema({ With this established we can remove our local array as we are ready to start getting our data from the database: ```diff -const Course = new mongoose.model('Course', courseSchema); +const Course = mongoose.model('Course', courseSchema); - const courses = [ - { @@ -191,3 +191,17 @@ router.delete("/:id", (req, res) => { res.send(course); }); ``` + +```diff + +router.delete("/:id", async (req, res) => { + const courseToDelete = await Course.findByIdAndRemove(req.params.id) + + if (!course) return res.status(404).send("A course with the given ID was not found"); + +- courses.indexOf(course); +- courses.splice(index, 1); + +res.send(course); +}) +```