Discord.js Getting data from a json file returns “undefined”

I’m trying to access data from a orders.json file it should list all “orders” however it says “undefined” when i try to do that as shown in the pic below
image
it also does the same thing when there aren’t any “orders” in the json file
image
the list command’s code:

const fsn = require("fs-nextra");
const Discord = require('discord.js');
module.exports = {
    name: 'list',
    description: 'List of all orders',
    aliases: ['allorders'],
    execute(message) {
        

        fsn.readJSON("./orders.json").then((orderDB) => {
            let orderString;
            for(let x in orderDB) {
                orderString = orderString + "`" + x + "`: " + orderDB[x].status + "\n";
                // add newline character at the end to display each "order" on a separate line
            }
            const exampleEmbed = new Discord.MessageEmbed()
                .setTitle('Here\'s a list of the current orders and their status.')
                .setDescription(orderString)
                .setTimestamp()
                .setFooter(message.member.user.tag, message.author.avatarURL());
            message.channel.send(exampleEmbed);
        });
    }
}

data in the json file are stored like this:

{
    "tip": {
        "orderID": "tip",
        "userID": "734532125021307001",
        "guildID": "745409671430668389",
        "guild": "Cybers Taco Stand Server",
        "channelID": "746423099871985755",
        "order": "a",
        "customer": "Aro#1221",
        "status": "Unclaimed",
        "ticketChannelMessageID": "not set"
    },
}

Should it be orderDB[x].tip.status?

no no no the orders.json file contains orders with random letters “tip” is one of the orders and i want the command to list them all from the json file

The problem is caused by not assigning any value to orderString when you declare it, and then proceed to use it as a string.
You can easily fix this by assigning orderString to an empty string at the moment of declaration; i.e. let orderString = ''.

The underlying reason for this is twofold:

  1. An unassigned variable will return undefined when it is being evaluated.
  2. When you concatenate a string with a non-string, the non-string is converted to a string.

Now, let us have a look at your for loop body:

orderString = orderString + '`' + x + '`: ' + orderDB[x].status + '\n'

At the first iteration of the for loop orderString is unassigned. As per point 1 the first iteration is therefore equal to:

orderString = undefined + '`' + x + '`: ' + orderDB[x].status + '\n'

Since undefined is a non-string, the first iteration of the loop can be further rewritten as per point 2:

orderString = 'undefined' + '`' + x + '`: ' + orderDB[x].status + '\n'

This is why undefined was prepended to the list in the first image.
For the second image, the same conversion is happening at .setDescription(orderString).

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.