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,26 +1189,22 @@ 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");
//check if the field exists
if (char[request.body.field] === undefined && char.data[request.body.field] === undefined) { let char = JSON.parse(charJSON)
console.error('Error: invalid field.'); //check if the field exists
response.status(400).send('Error: invalid field.'); if (char[request.body.field] === undefined && char.data[request.body.field] === undefined) {
return; console.error('Error: invalid field.');
} response.status(400).send('Error: invalid field.');
char[request.body.field] = request.body.value; return;
char.data[request.body.field] = request.body.value; }
char = JSON.stringify(char); char[request.body.field] = request.body.value;
return { char }; char.data[request.body.field] = request.body.value;
}).then(({ char }) => { let newCharJSON = JSON.stringify(char);
charaWrite(avatarPath, char, (request.body.avatar_url).replace('.png', ''), response, 'Character saved'); await charaWrite(avatarPath, newCharJSON, (request.body.avatar_url).replace('.png', ''), response, 'Character saved');
}).catch((err) => { } 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.');
} }
}); });