From e91a61cd929db5c02ff25573d9a7ef2ccc072d97 Mon Sep 17 00:00:00 2001 From: Alan Chamathil <77508072+AlanChamathil@users.noreply.github.com> Date: Mon, 27 Mar 2023 12:55:19 +0100 Subject: [PATCH] updated to async and await --- src/routes/notification.js | 70 +++++++++++++++----------------------- 1 file changed, 27 insertions(+), 43 deletions(-) diff --git a/src/routes/notification.js b/src/routes/notification.js index e2fb9e2..a1b1388 100644 --- a/src/routes/notification.js +++ b/src/routes/notification.js @@ -14,65 +14,49 @@ const Notification = require('../schema/schema') // get that repsond - delete the notifcations from the database -router.get("/info/:id",(req,res) =>{ - - +router.get("/info/:id", async (req,res) =>{ // grab the userid // retrieve all the notifcations stored in the database - - Notification.find({userid:req.params.id}).then((notifiaction) => { - if(!notifiaction){ - return res.status(404).send(); - } - res.send(notifiaction); - }).catch((error) => { - res.status(500).send(error); - }) - + try{ + const notifications = await Notification.find({userid:req.params.id}); + res.status(200).send(notifications); + }catch(error){ + res.status(500).send(error); + } + // send the notifications to the notification page - - }) + }); - router.delete("/delete/:id",(req,res) =>{ - //64216d2a4341c23ccfa83506 + router.delete("/delete/:id", async (req,res) =>{ // delete the notifications // grab the notification id // delete on the database - - Notification.findByIdAndDelete(req.params.id).then((notifiaction) => { - if(!notifiaction){ - return res.status(404).send(); - } - res.send(notifiaction); - }).catch((error) => { + try{ + const notifications = await Notification.findByIdAndDelete(req.params.id) + if(!notifications){ + return res.status(404).send(); + } + res.send(notifications); + }catch(error){ res.status(500).send(error); - }) + } - }) + }); - router.post("/create", (req,res) => { - + router.post("/create", async (req,res) => { //recieve the notifcation info from the user - - // const user_id = req.body['user_id']; - // const title = req.body['title']; - // const description = req.body['description']; - // const time = new Date(); - // send the notifcation to the database (create/save/insert methods) - - const notification = new Notification(req.body); - notification.save().then((notifiaction)=>{ - res.status(201).send(notifiaction) - }).catch((error)=>{ - res.status(400).send(error); - }) - + const notifications = new Notification(req.body); + try{ + await notifications.save(); + res.status(201).send(notifications); + } catch (error){ + res.status(400).send(error); + } }); - module.exports = router \ No newline at end of file -- GitLab