convert to async and fix types

This commit is contained in:
RealBeepMcJeep 2023-08-30 12:58:43 -07:00
parent 2e3ddd16d5
commit 9dfe42a7c3
1 changed files with 16 additions and 20 deletions

View File

@ -1189,8 +1189,10 @@ app.post("/editcharacterattribute", jsonParser, async function (request, respons
try { try {
const avatarPath = path.join(charactersPath, request.body.avatar_url); const avatarPath = path.join(charactersPath, request.body.avatar_url);
charaRead(avatarPath).then((char) => { let charJSON = await charaRead(avatarPath);
char = JSON.parse(char); if (typeof charJSON !== 'string') throw new Error("Failed to read character file");
let char = JSON.parse(charJSON)
//check if the field exists //check if the field exists
if (char[request.body.field] === undefined && char.data[request.body.field] === undefined) { if (char[request.body.field] === undefined && char.data[request.body.field] === undefined) {
console.error('Error: invalid field.'); console.error('Error: invalid field.');
@ -1199,16 +1201,10 @@ app.post("/editcharacterattribute", jsonParser, async function (request, respons
} }
char[request.body.field] = request.body.value; char[request.body.field] = request.body.value;
char.data[request.body.field] = request.body.value; char.data[request.body.field] = request.body.value;
char = JSON.stringify(char); let newCharJSON = JSON.stringify(char);
return { char }; await charaWrite(avatarPath, newCharJSON, (request.body.avatar_url).replace('.png', ''), response, 'Character saved');
}).then(({ char }) => { } catch (err) {
charaWrite(avatarPath, char, (request.body.avatar_url).replace('.png', ''), response, 'Character saved');
}).catch((err) => {
console.error('An error occured, character edit invalidated.', err); console.error('An error occured, character edit invalidated.', err);
});
}
catch {
console.error('An error occured, character edit invalidated.');
} }
}); });