Skip to content
Advertisement

How to access property of a property of a JSON object if the initial property is a variable?

I am making a discord bot and have successfully made a system where it stores user’s variables in a JSON file in this format:

{
  "801479782613123123": {
    "status": true
  },
  "725581416720629863": {
    "status": true
  }
}

My current problem is that I am unable to check this ‘status’ property is true or false when I pass in a variable as the initial property. Here is my code:

let statusPath = './commands/afkstatus.json';
    let statusRead = fs.readFileSync(statusPath);
    let statusFile = JSON.parse(statusRead); //Ready for use
    let userID = message.author.id
    if (message.mentions.users.first()) {
        let ping = message.mentions.users.first();
        let status = statusFile[userID.status];
        if (statusFile[userID.status]) {
            message.channel.send(`Message`);
        }
    }

When I run the code, there is no error message, the message simply does not get sent.

Advertisement

Answer

Try let status = statusFile[userID].status instead.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement