node.js - 映射 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
如果我不等待,一切都会返回未定义
解决方案
- 您可以
Discord.Collection()
在从 mongoose 数据库中获取数据后对数据进行排序。
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")
)
推荐阅读
- c++ - 通过 C++ 模板元编程从数组名推导出数组维数
- android - 在安装下载的 apk 文件时通过 Intent.getData() 错误暴露在应用程序之外
- php - 将 Symfony 项目部署到 App Engine - 错误:文件过多
- java - 使用 Java 在 CSV 中写入非拉丁字符
- angular - 测试覆盖范围不包括任何西装
- c - 无法在 Visual Studio 中打开源文件 err.h?
- python - 如何将混合日期格式 mm-dd-yyyy 和 yyyy-mm-dd 转换为 pandas 中的日期
- arrays - 将相同的对象添加到数组
- java - 使用 liferay 6.1 安排工作
- arrays - 在 ReactJs 中将 Json 数据添加到数组中