From 5524f14e1a5ce4d1ea4450b1cd09c00f1fc23450 Mon Sep 17 00:00:00 2001 From: grey Date: Sun, 9 Jul 2023 18:07:28 +0200 Subject: [PATCH] fixed error with duplicate stringify operation in api routes Co-authored-by: Spacelord --- src/routes/api/v1/categories.ts | 2 +- src/routes/api/v1/items.ts | 4 ++-- src/routes/api/v1/search/sku.ts | 2 +- src/routes/api/v1/storageLocations.ts | 4 ++-- src/routes/api/v1/storageUnits.ts | 4 ++-- static/js/editCategory.js | 4 +--- static/js/editItems.js | 3 +-- static/js/editStorages.js | 6 ++---- static/js/formHandler.js | 3 +-- static/js/itemPageHandler.js | 3 +-- static/js/searchBox.js | 3 +-- 11 files changed, 15 insertions(+), 23 deletions(-) diff --git a/src/routes/api/v1/categories.ts b/src/routes/api/v1/categories.ts index 90d5ed5..50088c0 100644 --- a/src/routes/api/v1/categories.ts +++ b/src/routes/api/v1/categories.ts @@ -17,7 +17,7 @@ function get(req: Request, res: Response) { }) .then((item) => { if (item) { - res.status(200).json(JSON.stringify(item)); + res.status(200).json(item); } else { res.status(410).json({ errorcode: 'NOT_EXISTING', error: 'Category does not exist' }); } diff --git a/src/routes/api/v1/items.ts b/src/routes/api/v1/items.ts index 57487ea..0ee500d 100644 --- a/src/routes/api/v1/items.ts +++ b/src/routes/api/v1/items.ts @@ -43,7 +43,7 @@ async function get(req: Request, res: Response) { }) .then((items) => { if (items) { - res.status(200).json(JSON.stringify(items)); + res.status(200).json(items); } else { res.status(410).json({ errorcode: 'NOT_EXISTING', error: 'Item does not exist' }); } @@ -117,7 +117,7 @@ async function get(req: Request, res: Response) { }) .then((items) => { if (items) { - res.status(200).json(JSON.stringify({ total: itemCountFiltered, totalNotFiltered: itemCountNotFiltered, items: items })); + res.status(200).json({ total: itemCountFiltered, totalNotFiltered: itemCountNotFiltered, items: items }); } else { res.status(410).json({ errorcode: 'NOT_EXISTING', error: 'Item does not exist' }); } diff --git a/src/routes/api/v1/search/sku.ts b/src/routes/api/v1/search/sku.ts index cdc3d36..bc887e5 100644 --- a/src/routes/api/v1/search/sku.ts +++ b/src/routes/api/v1/search/sku.ts @@ -19,7 +19,7 @@ function get(req: Request, res: Response) { } }) .then((items) => { - res.status(200).json(JSON.stringify(items)); + res.status(200).json(items); }) .catch((err) => { log.db.error(err); diff --git a/src/routes/api/v1/storageLocations.ts b/src/routes/api/v1/storageLocations.ts index c076cfc..5ebd5e5 100644 --- a/src/routes/api/v1/storageLocations.ts +++ b/src/routes/api/v1/storageLocations.ts @@ -21,7 +21,7 @@ function get(req: Request, res: Response) { }) .then((items) => { if (items) { - res.status(200).json(JSON.stringify(items)); + res.status(200).json(items); } else { res.status(410).json({ errorcode: 'NOT_EXISTING', error: 'storageLocation does not exist' }); } @@ -40,7 +40,7 @@ function get(req: Request, res: Response) { }) .then((items) => { if (items) { - res.status(200).json(JSON.stringify(items)); + res.status(200).json(items); } else { res.status(410).json({ errorcode: 'NOT_EXISTING', error: 'storageLocation does not exist' }); } diff --git a/src/routes/api/v1/storageUnits.ts b/src/routes/api/v1/storageUnits.ts index 5003395..4884407 100644 --- a/src/routes/api/v1/storageUnits.ts +++ b/src/routes/api/v1/storageUnits.ts @@ -23,7 +23,7 @@ function get(req: Request, res: Response) { }) .then((items) => { if (items) { - res.status(200).json(JSON.stringify(items)); + res.status(200).json(items); } else { res.status(410).json({ errorcode: 'NOT_EXISTING', error: 'storageUnit does not exist' }); } @@ -43,7 +43,7 @@ function get(req: Request, res: Response) { }) .then((items) => { if (items) { - res.status(200).json(JSON.stringify(items)); + res.status(200).json(items); } else { res.status(410).json({ errorcode: 'NOT_EXISTING', error: 'storageUnit does not exist' }); } diff --git a/static/js/editCategory.js b/static/js/editCategory.js index 45934f6..dbeb4a1 100644 --- a/static/js/editCategory.js +++ b/static/js/editCategory.js @@ -2,9 +2,7 @@ function getDataForEdit(name) { $.ajax({ type: 'get', url: `/api/v1/categories?name=${name}`, - success: function (data) { - const result = JSON.parse(data); - + success: function (result) { // Get elements inside the editCategoryModal const modal_categoryName = document.getElementById('editCategoryModalName'); const modal_categoryDescription = document.getElementById('editCategoryModalDescription'); diff --git a/static/js/editItems.js b/static/js/editItems.js index e1504ac..e40c163 100644 --- a/static/js/editItems.js +++ b/static/js/editItems.js @@ -19,8 +19,7 @@ function getDataForEdit(id) { $.ajax({ type: 'get', url: `/api/v1/items?id=${id}`, - success: function (data) { - const result = JSON.parse(data); + success: function (result) { // Get elements inside the editCategoryModal const modal_itemName = document.getElementById('itemModifyModalName'); diff --git a/static/js/editStorages.js b/static/js/editStorages.js index 54b2f9a..e227d97 100644 --- a/static/js/editStorages.js +++ b/static/js/editStorages.js @@ -68,8 +68,7 @@ function getDataForEdit(id) { $.ajax({ type: 'get', url: `/api/v1/storageUnits?id=${id}`, - success: function (data) { - const result = JSON.parse(data); + success: function (result) { // Get elements inside the editCategoryModal const modal_unitName = document.getElementById('storageUnitModalName'); @@ -107,8 +106,7 @@ function getDataForEditLoc(id) { $.ajax({ type: 'get', url: `/api/v1/storageLocations?id=${id}`, - success: function (data) { - const result = JSON.parse(data); + success: function (result) { // Get elements inside the editCategoryModal const modal_locationName = document.getElementById('storageLocationModalName'); diff --git a/static/js/formHandler.js b/static/js/formHandler.js index 900a84a..47fdbeb 100644 --- a/static/js/formHandler.js +++ b/static/js/formHandler.js @@ -108,8 +108,7 @@ function preFillDeleteModalNxt(id, route, name, requestIdent='id') { $.ajax({ type: 'get', url: `/api/v1/${route}?${requestIdent}=${id}`, - success: function (data) { - const result = JSON.parse(data); + success: function (result) { // Get elements inside the editCategoryModal const modal_categoryName = document.getElementById('deleteNamePlaceholder'); diff --git a/static/js/itemPageHandler.js b/static/js/itemPageHandler.js index 0353334..11aef43 100644 --- a/static/js/itemPageHandler.js +++ b/static/js/itemPageHandler.js @@ -23,8 +23,7 @@ function loadPageData() { }, 1000); } -function dataResponseHandler(res) { - json = JSON.parse(res); +function dataResponseHandler(json) { // console.log(json) totalNotFiltered = json.totalNotFiltered; total = json.total; diff --git a/static/js/searchBox.js b/static/js/searchBox.js index 635fca1..a814326 100644 --- a/static/js/searchBox.js +++ b/static/js/searchBox.js @@ -67,8 +67,7 @@ function handleSearchChange(e) { $.ajax({ type: 'get', url: url, - success: function (data) { - let result = JSON.parse(data); + success: function (result) { let htmlResult = "" result.forEach(element => { console.log(element);