简体   繁体   中英

Leaderboard.map is not a function when trying to create a levels leaderboard

I'm trying to get a leaderboard for XP/levels up and running (with the data stored in mongoDB) and keep getting this typeError (leaderboard.map is not a function) whenever I run the command.

I'm able to check the levels using a,rank command. but run into issues with the !leaderboard command.

My leaderboard code is:

    if(command === "leaderboard" || command === "lb") {
        const rawLeaderboard = await Levels.fetchLeaderboard(message.guild.id, 5);
        if (rawLeaderboard.length < 1) return reply("PandaBot says nobody's in leaderboard yet! 🐼");

        const leaderboard = Levels.computeLeaderboard(client, rawLeaderboard); 

        const lb = leaderboard.map(e => `${e.position}. ${e.username}#${e.discriminator}\nLevel: ${e.level}\nXP: ${e.xp.toLocaleString()}`);

        message.channel.send(`${lb.join("\n\n")}}`)
    }

All I can think of is changing it to await leaderboard.map but I won't have access to the computer to test it for a while.

Any help with this would be greatly appreciated!

Levels.computeLeaderboard(client, rawLeaderboard) returns a promise so you either need to use .then() or await to get the result:

const leaderboard = await Levels.computeLeaderboard(client, rawLeaderboard);
const lb = leaderboard.map(
  (e) =>
    `${e.position}. ${e.username}#${e.discriminator}\nLevel: ${
      e.level
    }\nXP: ${e.xp.toLocaleString()}`,
);

message.channel.send(lb.join('\n\n'));

Nevermind. Added await Levels.computeLeaderboard and it worked

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM