diff options
| -rw-r--r-- | src/db.js | 7 |
1 files changed, 4 insertions, 3 deletions
@@ -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){ |
