From bc220cc3cef8456e5de146288d3ec567153ca8e3 Mon Sep 17 00:00:00 2001 From: Joe Carstairs <65492573+Sycamost@users.noreply.github.com> Date: Thu, 7 Mar 2024 22:33:55 +0000 Subject: Fixes fs problems --- src/db.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'src/db.js') diff --git a/src/db.js b/src/db.js index 16278c4..52652fd 100644 --- a/src/db.js +++ b/src/db.js @@ -1,7 +1,7 @@ // Ideally, we should write to a file and read it at app start in case of crashes. // An actual SQL database would probably be overkill. -const { readFileSync, writeFile } = require("fs"); +const fs = require("fs"); /** @type Array<{ name: string, number: string }> */ const subscriptions = []; @@ -42,7 +42,7 @@ function getSubscriptionFromNumber(number) { async function save() { try { - await writeFile('./data.json', JSON.stringify({ subscriptions })); + await fs.promises.writeFile('./data.json', JSON.stringify({ subscriptions })); } catch (err) { if (typeof err === 'string') { console.error(`Error writing data: ${err}`); @@ -54,7 +54,8 @@ async function save() { function load() { try { - const data = JSON.parse(readFileSync('./data.json')); + const json = fs.readFileSync('./data.json'); + const data = JSON.parse(json); while (subscriptions.pop()) {} subscriptions.push(...data['subscriptions']); } catch (err){ -- cgit v1.2.3