From 7318b7445392977ee34681512efe653df0fcda1b Mon Sep 17 00:00:00 2001 From: Redume Date: Tue, 29 Oct 2024 21:19:26 +0300 Subject: [PATCH] fix(main): if there was no data, an empty array was returned --- server/routes/getRate.js | 13 +++++++++++-- shared/database/src/main.js | 4 ++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/server/routes/getRate.js b/server/routes/getRate.js index 28be5d3..6a1105e 100644 --- a/server/routes/getRate.js +++ b/server/routes/getRate.js @@ -10,15 +10,16 @@ module.exports = async function getRateRoute(fastify) { 'The from_currency and conv_currency fields are required', }); } + let rate_res; if (query['date']) - return rate.getDay( + rate_res = await rate.getDay( query['from_currency'], query['conv_currency'], query['date'], ); else if (query['start_date'] && query['end_date']) - return rate.getPeriod( + rate_res = await rate.getPeriod( query['from_currency'], query['conv_currency'], query['start_date'], @@ -32,5 +33,13 @@ module.exports = async function getRateRoute(fastify) { "There must be fields 'date' or 'start_date' and 'end_date'. " + 'Read more in the documentation', }); + + if (typeof rate_res !== "object") return res.status(400).send({ + status: 400, + message: rate_res, + }); + else return res.status(200).send( + rate_res + ) }); }; diff --git a/shared/database/src/main.js b/shared/database/src/main.js index cbfeae7..13bd594 100644 --- a/shared/database/src/main.js +++ b/shared/database/src/main.js @@ -20,7 +20,7 @@ async function getDay(from_currency, conv_currency, date) { [from_currency.toUpperCase(), conv_currency.toUpperCase(), date], ); - if (!data) return new Error('Missing data'); + if (data?.['rows'].length <= 0) return 'Missing data'; const set_date = data['rows'][0]['date']; data['rows'][0]['date'] = new Date( @@ -58,7 +58,7 @@ async function getPeriod(from_currency, conv_currency, start_date, end_date) { ], ); - if (!data) return new Error('Missing data'); + if (data?.['rows'].length <= 0) return 'Missing data'; for (let i = 0; i < data['rows'].length; i++) { let date = data['rows'][i]['date'];