From 9dfe42a7c320400391eb4cbe0e70fa9eaa47ac39 Mon Sep 17 00:00:00 2001 From: RealBeepMcJeep Date: Wed, 30 Aug 2023 12:58:43 -0700 Subject: [PATCH] convert to async and fix types --- server.js | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/server.js b/server.js index 3631a5c9f..d27081530 100644 --- a/server.js +++ b/server.js @@ -1189,26 +1189,22 @@ app.post("/editcharacterattribute", jsonParser, async function (request, respons try { const avatarPath = path.join(charactersPath, request.body.avatar_url); - charaRead(avatarPath).then((char) => { - char = JSON.parse(char); - //check if the field exists - if (char[request.body.field] === undefined && char.data[request.body.field] === undefined) { - console.error('Error: invalid field.'); - response.status(400).send('Error: invalid field.'); - return; - } - char[request.body.field] = request.body.value; - char.data[request.body.field] = request.body.value; - char = JSON.stringify(char); - return { char }; - }).then(({ char }) => { - charaWrite(avatarPath, char, (request.body.avatar_url).replace('.png', ''), response, 'Character saved'); - }).catch((err) => { - console.error('An error occured, character edit invalidated.', err); - }); - } - catch { - console.error('An error occured, character edit invalidated.'); + let charJSON = await charaRead(avatarPath); + if (typeof charJSON !== 'string') throw new Error("Failed to read character file"); + + let char = JSON.parse(charJSON) + //check if the field exists + if (char[request.body.field] === undefined && char.data[request.body.field] === undefined) { + console.error('Error: invalid field.'); + response.status(400).send('Error: invalid field.'); + return; + } + char[request.body.field] = request.body.value; + char.data[request.body.field] = request.body.value; + let newCharJSON = JSON.stringify(char); + await charaWrite(avatarPath, newCharJSON, (request.body.avatar_url).replace('.png', ''), response, 'Character saved'); + } catch (err) { + console.error('An error occured, character edit invalidated.', err); } });