How to fix: quot;TypeError: Cannot read property #39;tag#39; of undefinedquot; error in discord.js(如何修复:“TypeError:无法读取未定义的属性‘标签’discord.js 中的错误)
问题描述
我正在制作一个 discord.js 机器人,并在其中放入了排行榜命令.
I'm making a discord.js bot and I've put a leaderboard command in it.
const Discord = require("discord.js");
// Get a filtered list (for this guild only), and convert to an array while we're at it.
const filtered = client.points.filter( p => p.guild === message.guild.id ).array();
// Sort it to get the top results... well... at the top. Y'know.
const sorted = filtered.sort((a, b) => b.points - a.points);
// Slice it, dice it, get the top 10 of it!
const top10 = sorted.splice(0, 10);
// Now shake it and show it! (as a nice embed, too!)
const embed = new Discord.RichEmbed()
.setTitle("Leaderboard")
.setAuthor(client.user.username, client.user.avatarURL)
.setDescription("Our top 10 points leaders!")
.setColor(0xff0000);
for(const data of top10) {
embed.addField(client.users.get(data.user).tag, `${data.points} points (level ${data.level})`);
}
return message.channel.send({embed});
}
但是当我运行命令时,我在命令行/日志中得到了这个:
But when I run the command I get this in the command line/logs:
embed.addField(client.users.get(data.user).tag, `${data.points} points (level ${data.level})`);
^
TypeError: Cannot read property 'tag' of undefined
at Object.exports.run (/app/commands/leaderboard.js:19:47)
at module.exports (/app/events/message.js:19:7)
at emitOne (events.js:121:20)
at Client.emit (events.js:211:7)
at MessageCreateHandler.handle (/rbd/pnpm-volume/461f582b-2c68-44a2-912c-88cd1fefb0fa/node_modules/.registry.npmjs.org/discord.js/11.4.2/node_modules/discord.js/src/client/websocket/packets/handlers/MessageCreate.js:9:34)
at WebSocketPacketManager.handle (/rbd/pnpm-volume/461f582b-2c68-44a2-912c-88cd1fefb0fa/node_modules/.registry.npmjs.org/discord.js/11.4.2/node_modules/discord.js/src/client/websocket/packets/WebSocketPacketManager.js:103:65)
at WebSocketConnection.onPacket (/rbd/pnpm-volume/461f582b-2c68-44a2-912c-88cd1fefb0fa/node_modules/.registry.npmjs.org/discord.js/11.4.2/node_modules/discord.js/src/client/websocket/WebSocketConnection.js:333:35)
at WebSocketConnection.onMessage (/rbd/pnpm-volume/461f582b-2c68-44a2-912c-88cd1fefb0fa/node_modules/.registry.npmjs.org/discord.js/11.4.2/node_modules/discord.js/src/client/websocket/WebSocketConnection.js:296:17)
at WebSocket.onMessage (/rbd/pnpm-volume/461f582b-2c68-44a2-912c-88cd1fefb0fa/node_modules/.registry.npmjs.org/ws/4.1.0/node_modules/ws/lib/event-target.js:120:16)
at emitOne (events.js:116:13)
我已将机器人从我的电脑转移到 glitch.com,这似乎修复了一段时间,但现在问题又回来了.
I've transferred the bot from my pc to glitch.com which seemed to fix it for a while, but now the problem is back.
推荐答案
这个错误意味着 client.users.get(data.user)
返回 undefined,因此不能有属性 标记
就可以了.大概这意味着data中指定的用户不存在.
The error means client.users.get(data.user)
is returning undefined, hence there cannot be a property tag
on it. Presumably this means the user specified in data does not exist.
您应该在直接尝试访问该属性之前添加一些防御性代码,以确保 get()
调用返回一些内容而不是 undefined.
You should add some defensive code before directly trying to access the property to ensure that the get()
call returns something rather than undefined.
例如
const user = client.users.get(data.user);
if (user && user.tag) {
// code here
} else {
// user does not exist..
}
这篇关于如何修复:“TypeError:无法读取未定义的属性‘标签’"discord.js 中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何修复:“TypeError:无法读取未定义的属性‘标签


基础教程推荐
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- 直接将值设置为滑块 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01