const { MessageEmbed } = require('discord.js');
  const fs = require('fs');
  const db = require('quick.db');
  const bot = ('Discord.Client')
  module.exports = {
      name: 'leaderboard',
  
  description: "idk something",
execute(message, args) {
    let money = db.all().filter(data => data.ID.startsWith(`money_`)).sort((a, b) => b.data - a.data);
    console.log(money)
            if (!money.length) {
                let noEmbed = new MessageEmbed()
                    .setAuthor(message.member.displayName, message.author.displayAvatarURL())
                    .setColor("BLUE")
                    .setFooter("No leaderboard")
                return message.channel.send(noEmbed)
            };
    
            money.length = 10;
            var finalLb = "";
            for (var i in money) {
                let currency1;
                let fetched = db.fetch(`money_${message.guild.id}`);
                if (fetched == null) {
                    currency1 = ''
                } else {
                    currency1 = fetched
                }
                if (money[i].data === null) {money[i].data = 0}
                finalLb += `**${money.indexOf(money[i]) + 1}.${message.guild.members.fetch(money.ID.split('_')[1]) ? message.guild.members.fetch(money.ID.split('_')[1]).tag : ""}** - ${money.data} ${currency1}\n`;
            };
    
            const embed = new MessageEmbed()
                .setTitle(message.guild.name)
                .setColor("BLUE")
                .setDescription(finalLb)
                .setTimestamp()
                .setFooter('Command: !help for currency commands')
            message.channel.send(embed);
  }
  }

trying to make a leaderboard command and got this error TypeError: Cannot read property 'split' of undefined. ive been looking for answers for a while but so far i havent seen anything specific to my situation. can anyone help?

1

There are 1 best solutions below

2
On

It seems like you're making a lot of asynchronous calls in the function. What is probably happening is that the string value that you're calling the split function does not exist because the data from the server hasn't reached the client.

To fix this, add the keyword "async" before your function name (execute) and add the keyword "await" before all the function calls to db

async execute(message, args){
...
}

and

    let money = await db.all().filter(data => data.ID.startsWith(`money_`)).sort((a, b) => b.data - a.data);

     let fetched = await db.fetch(`money_${message.guild.id}`);

I also recommend that you read about asynchronous functions in javascript