首页 > 解决方案 > 映射 discordjs mongoose 时显示用户标签

问题描述

所以我正在对排行榜进行排序,它当前正在显示用户的 ID。我希望它显示用户标签,然后显示他们的余额。

我有这个代码

    const lb = users
              .slice(0)
              .sort(({ Bobux: a }, { Bobux: b }) => b - a)
              .map(
                
                ({ User, Bobux }, pos) => `${pos + 1}. <@${ await client.users.fetch(User).tag}> - ${commaNumber(Bobux)} Bobux`,
              );

但我得到了错误

(node:13960) UnhandledPromiseRejectionWarning: C:\Users\Sochum\Desktop\BobloxBot\commands\leaderboard.js:26
                ({ User, Bobux }, pos) => `${pos + 1}. <@${ await client.users.fetch(User).tag}> - ${commaNumber(Bobux)} Bobux`,
SyntaxError: Missing } in template expression

在排序和显示前 15 个用户时如何显示用户的标签?用户 id 的变量是User

如果我不等待,一切都会返回未定义

标签: node.jsmongoosediscord.js

解决方案


const { Collection } = require("discord.js")
const collection = new Collection()

// collect every member's data in the guild
await Promise.all(
   message.guild.members.cache.map(async(member) => {
      const id = member.id
      const data = await <Schema>.findOne({ User: id }) // replace "<Schema>" with your Schema
      const currency = data.Bobux

   return collection.set(id, { id, currency })
   })
)

const lb = collection.sort((x, y) => y.currency - x.currency).first(15) // top 15 of the collected data

// destructing our data to send it in one message
message.channel.send(lb.map((v, i) => {
   return `\`${i + 1}\`. ${client.users.cache.get(v.id).tag}: ${v.currency}`
  }).join("\n")
)


推荐阅读