From 43e8497d67f0728726aa89a89386cbccdd73a820 Mon Sep 17 00:00:00 2001 From: moepoi Date: Sun, 21 Nov 2021 08:46:39 +0700 Subject: [PATCH] Add error handler for product page --- routes/index.js | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/routes/index.js b/routes/index.js index f4dec25..e4d6fb7 100644 --- a/routes/index.js +++ b/routes/index.js @@ -180,24 +180,32 @@ router.get("/products", (req, res) => { }); }); -router.get("/product/:id", (req, res) => { - auth.session_converter(req.cookies.session_token).then((key) => { - if (key != null) { - controller.getProduct(req.params.id).then((data) => { - res.render("pages/product", { - data: data, - loggedIn: 'true' +router.get(["/product", "/product/:id"], (req, res) => { + if (!req.params.id) { + auth.session_converter(req.cookies.session_token).then((key) => { + if (key != null) { + controller.getProduct(req.params.id).then((data) => { + if (data != undefined) { + res.render("pages/product", { + data: data, + loggedIn: 'true' + }); + } else { + res.redirect("/products"); + } }); - }); - } else { - controller.getProduct(req.params.id).then((data) => { - res.render("pages/product", { - data: data, - loggedIn: 'false' + } else { + controller.getProduct(req.params.id).then((data) => { + res.render("pages/product", { + data: data, + loggedIn: 'false' + }); }); - }); - } - }); + } + }); + } else { + res.redirect("/products") + } }); router.get("/bag", (req, res) => {