From b30de5bf786ae8f28f3248277c5b2df2f0e5ebf4 Mon Sep 17 00:00:00 2001 From: KernelDeimos Date: Tue, 29 Oct 2024 13:18:53 -0400 Subject: [PATCH] fix: reading JSON string from service_usage_monthly This was hilariously difficult to debug. This isn't the first time an issue like this occurred; this happens because of a deviation between mysql and sqlite, where a JSON-typed column in mysql will return as a native object in queries, whereas a JSON-typed column in sqlite is a string column and will therefore return as an un-parsed JSON string in queries. --- src/backend/src/routers/drivers/usage.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/backend/src/routers/drivers/usage.js b/src/backend/src/routers/drivers/usage.js index 36fd1d4ef..c5beb27cd 100644 --- a/src/backend/src/routers/drivers/usage.js +++ b/src/backend/src/routers/drivers/usage.js @@ -60,11 +60,18 @@ module.exports = eggspress('/drivers/usage', { for ( const row of rows ) { const app = await get_app({ id: row.app_id }); - + let extra_parsed; try { - extra_parsed = JSON.parse(row.extra); + extra_parsed = db.case({ + mysql: () => row.extra, + otherwise: () => JSON.parse(row.extra), + })(); } catch ( e ) { + console.log( + '\x1B[31;1m error parsing monthly usage extra', + row.extra, e, + ); continue; }