我收到一个TypeError,但应该不是,因为我相信我已经正确定义了变量(请纠正我!)

已经四处寻找并找到它了;但是,即使有了“修复程序”,它仍然给了我错误。对于我绝对犯的错误,我深表歉意!

var parts = message.content.split(" ");
if (message.content.startsWith(prefix + "sr")) {
  const servguild = message.guild.id;
  const member = message.mentions.members.first();
  const role = servguild.roles.find('name', parts[2]);
  member.addRole(role);
}

最佳答案

您已将servguild定义为message.guild.id;,并试图访问它的roles属性,因此试图访问message.guild.id.roles。但是,roles属性仅存在于guild属性上,而不存在于guild.id属性上。

Discord Developer Portal Docs for Guild,您可以看到公会的定义如下:

{
    "id": "41771983423143937",
    "application_id": null,
    "name": "Discord Developers",
    "icon": "86e39f7ae3307e811784e2ffd11a7310",
    "splash": null,
    "owner_id": "80351110224678912",
    "region": "us-east",
    "afk_channel_id": "42072017402331136",
    "afk_timeout": 300,
    "embed_enabled": true,
    "embed_channel_id": "41771983444115456",
    "verification_level": 1,
    "default_message_notifications": 0,
    "explicit_content_filter": 0,
    "mfa_level": 0,
    "widget_enabled": false,
    "widget_channel_id": "41771983423143937",
    "roles": [],
    "emojis": [],
    "features": ["INVITE_SPLASH"],
    "unavailable": false
}


要修复您的代码,请将const servguild = message.guild.id;更改为const servguild = message.guild;

关于node.js - 定义变量后,无法读取未定义的属性“查找”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56123577/

10-10 03:06