I am making a discord bot and am working on a warn command. I am using a JSON file to store user data e.g previous warns. I would like to make the name of the string a variable e.g
{"826309574764134411":{"name":"Fortnite pro101","reason":"playing fortnite"}}
What I have tried is:
// create a JSON object
const userinfo = {
"name": warned.displayName,
"reason": reason
};
const userinfo2 = {
`${warned.id}`: userinfo
}
const data = JSON.stringify(userinfo2);
// write JSON string to a file
fs.writeFile("./user-data.json", (data), (err) => {
if (err) {
message.channel.send("Error: Unable to write data")
throw err;
}
console.log("JSON data is saved.");
});
and
// create a JSON object
const userinfo = {
"name": warned.displayName,
"reason": reason
};
const userinfo2 = {
warned.id: userinfo
}
const data = JSON.stringify(userinfo2);
// write JSON string to a file
fs.writeFile("./user-data.json", (data), (err) => {
if (err) {
message.channel.send("Error: Unable to write data")
throw err;
}
console.log("JSON data is saved.");
});
Both of them result in syntax errors. Sorry if this question is bad this is my first question as I dont like asking for help. help would be greatlk appreaciated :)
edit: error:
Process exited with code 1
C:\Program Files\nodejs\node.exe .\main.js
Debugger attached.
Waiting for the debugger to disconnect...
c:\Users\xavie\OneDrive\Desktop\BOT - Project\Mushroom's server [test 1]\commands\warn.js:30
warned.id: userinfo
^
SyntaxError: Unexpected token '.'
at wrapSafe (internal/modules/cjs/loader.js:984:16)
at Module._compile (internal/modules/cjs/loader.js:1032:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
at Module.load (internal/modules/cjs/loader.js:933:32)
at Function.Module._load (internal/modules/cjs/loader.js:774:14)
at Module.require (internal/modules/cjs/loader.js:957:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object.<anonymous> (c:\Users\xavie\OneDrive\Desktop\BOT - Project\Mushroom's server [test 1]\main.js:13:21)
at Module._compile (internal/modules/cjs/loader.js:1068:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
Process exited with code 1
Canceled