开发者问题收集

如何修复 NodeJS 中的“TypeError: 无法读取未定义的属性‘tag’”错误

2019-04-09
2836

我是编码新手,我正在尝试在 Discord 上设置一个机器人……我希望机器人通过嵌入向用户发送消息,类似于我之前在项目中使用的一个,它工作得很好。我稍微修改了代码,但它不会注册这个,即使其他代码工作得很好。我做错了什么?错误消息如下:

TypeError: Cannot read property 'tag' of undefined
    at Client.client.on (C:\Users\Tristan\my-bot\pokegrove\index.js:112:29)
    at Client.emit (events.js:194:15)
    at MessageCreateHandler.handle (C:\Users\Tristan\my-bot\pokegrove\node_modules\discord.js\src\client\websocket\packets\handlers\MessageCreate.js:9:34)
    at WebSocketPacketManager.handle (C:\Users\Tristan\my-bot\pokegrove\node_modules\discord.js\src\client\websocket\packets\WebSocketPacketManager.js:103:65)
    at WebSocketConnection.onPacket (C:\Users\Tristan\my-bot\pokegrove\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:333:35)
    at WebSocketConnection.onMessage (C:\Users\Tristan\my-bot\pokegrove\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:296:17)
    at WebSocket.onMessage (C:\Users\Tristan\my-bot\pokegrove\node_modules\ws\lib\event-target.js:120:16)
    at WebSocket.emit (events.js:189:13)
    at Receiver._receiver.onmessage (C:\Users\Tristan\my-bot\pokegrove\node_modules\ws\lib\websocket.js:137:47)
    at Receiver.dataMessage (C:\Users\Tristan\my-bot\pokegrove\node_modules\ws\lib\receiver.js:409:14)

我尝试更改原始代码的部分内容以满足我的需求。我搜索了与我收到的错误代码有关的其他问题,但找不到任何特定于我的问题的内容……这是我使用的原始代码(这个工作得很好):

client.on("guildMemberAdd", (member) => { // Check out previous chapter for information about this event
let guild = member.guild; 
let memberTag = member.user.tag; 
if(guild.systemChannel){
    guild.systemChannel.send(new Discord.RichEmbed() // Creating instance of Discord.RichEmbed
    .setTitle("A new user joined") // Calling method setTitle on constructor. 
    .setDescription(memberTag + " has joined the adventure") // Setting embed description
    .setThumbnail(member.user.displayAvatarURL) // The image on the top right; method requires an url, not a path to file!
    .addField("Members now", member.guild.memberCount) // Adds a field; First parameter is the title and the second is the value.
    .setTimestamp() // Sets a timestamp at the end of the embed
    );
}
});

忽略此代码上的“addfield”,我仍在更改它(即使没有添加它,它也存在同样的问题,所以这似乎无关紧要)。这是我稍作修改的代码(不起作用):

client.on('message', (member) => { // Check out previous chapter for information about this event
let guild = member.guild; 
let memberTag = member.user.tag; 
if(prefix + "donate"){
    message.author.send(new Discord.RichEmbed() // Creating instance of Discord.RichEmbed
    .setTitle("Thank you for your support!") // Calling method setTitle on constructor. 
    .setDescription(memberTag + ", with your [donation](https://www.paypal.me/pokegroveofficial), we can continue to better PokéGrove (and buy some more Poké treats!)") // Setting embed description
    .setThumbnail("http://i68.tinypic.com/2ltq9nt.jpg") // The image on the top right; method requires an url, not a path to file!
    .setImage("http://i68.tinypic.com/2ltq9nt.jpg")
    .addField("Members now", member.guild.memberCount) // Adds a field; First parameter is the title and the second is the value.
    .setTimestamp() // Sets a timestamp at the end of the embed
    );
}
});

我期望代码能够发送嵌入式直接消息,但相反,机器人崩溃并出现该错误。任何帮助都将不胜感激,因为我已经通过 Google 搜索并尝试向其他人寻求帮助,但我基本上被告知“自己想办法”。

2个回答

在此行

let memberTag = member.user.tag;

缺少属性 user。

您应该添加一些防御性代码来处理这种情况,或者确定它不存在的原因。

类似于

if (member.user && member.user.tag) {
 // access tag here
} else {
 // doesn’t exist
}
Matt
2019-04-10

如果我们谈论的是您的第二段代码,则消息事件中没有 member 参数。它将是 message ,并且消息没有 user 属性。这就是错误的根源。

client.on('message', message => {
    let guild = message.guild;
    let memberTag = message.author.tag;

    // rest of code
});

Discord.js 文档 可能会有所帮助。

slothiful
2019-04-11