convert to async and fix types
This commit is contained in:
parent
2e3ddd16d5
commit
9dfe42a7c3
18
server.js
18
server.js
|
@ -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.');
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue