【问题标题】:Display User Tag While Mapping discordjs mongoose映射 discordjs mongoose 时显示用户标签
【发布时间】:2021-11-16 10:20:55
【问题描述】:

所以我正在对排行榜进行排序,它当前正在显示用户的 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.js mongoose discord.js


    【解决方案1】:
    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")
    )
    
    

    【讨论】:

    • 我收到错误 TypeError: Cannot read property 'Bobux' of null 使用该代码时(我正确替换了所有内容)
    • 尝试删除awaitconst data = &lt;Schema&gt;.findOne({ User: id })
    猜你喜欢
    • 1970-01-01
    • 2021-03-27
    • 2021-12-22
    • 1970-01-01
    • 2020-10-15
    • 1970-01-01
    • 2019-05-14
    • 1970-01-01
    • 2018-05-07
    相关资源
    最近更新 更多