简体   繁体   English

Node.js 捕获错误未定义但已定义

[英]Node.js caught error undefined but defined

I have been working on a bot for 12 days now and I need to upgrade to v12.我已经在机器人上工作了 12 天,我需要升级到 v12。 Everything was going smoothly until I tried a command that will give a specified role to a mentioned user.一切都很顺利,直到我尝试了一个命令,该命令将为提到的用户提供指定的角色。

I have 2 variables.我有 2 个变量。 One is a role variable that the user has specified and the member variable (which is equaled to message.guild.members.get(args[0])).一个是用户指定的角色变量和成员变量(等于 message.guild.members.get(args[0]))。

The code looks like this:代码如下所示:

let Member = message.mentions.members.first() || message.guild.members.get(arguments[0]);
    if (!Member) return message.channel.send("Error: Either the member does not exist or you haven't put the member to give the role to!");
    let role = message.guild.roles.cache.some(r => r.name === arguments[1]) || message.guild.roles.cache.some(r => r.id === arguments[1]) || message.mentions.roles.first();
    if (!role) return nessage.channel.send("Error: Please provide a valid role!");
    
    // if (role === "undefined") {
        // let role = message.guild.roles.cache.some(r => r.name === arguments[1]) || message.guild.roles.cache.some(r => r.id === arguments[1]) || message.mentions.roles.first();
    // }
    
    if (Member.roles.cache.has(role.id)) {
        return message.channel.send("Error: The mentioned user already has that role!");
    } else {
        let role = message.guild.roles.cache.some(r => r.name === arguments[1]) || message.guild.roles.cache.some(r => r.id === arguments[1]) || message.mentions.roles.first();
        console.log(message);
        console.log("role" + role);
        Member.roles.add(role.id).catch(e => console.log("Error: " + e.message));
    }

The code looked fine to me and expected it to work, but then I got an error in the console.该代码对我来说看起来不错,并希望它可以工作,但后来我在控制台中遇到了错误。 It doesn't say what variable is not defined, it just says it's undefined.它没有说什么变量没有定义,它只是说它是未定义的。

I tried checking if the role variable is undefined, but I commented out since it didn't work.我尝试检查角色变量是否未定义,但我注释掉了,因为它不起作用。 Then, I have let it print the message and check if the role is undefined.然后,我让它打印消息并检查角色是否未定义。 The role variable is fine.角色变量很好。 The message variable is fine.消息变量很好。 Even the Member variable is fine.甚至 Member 变量也很好。

I use the latest Node.js.我使用最新的 Node.js。

Here's the console log:这是控制台日志:

Message {
  channel: TextChannel {
    type: 'text',
    deleted: false,
    id: '729663938148565134',
    name: 'general',
    rawPosition: 0,
    parentID: '729663938148565132',
    permissionOverwrites: Collection [Map] {},
    topic: 'Beware, there will be a lot of mentions.',
    nsfw: undefined,
    lastMessageID: '733951355240316948',
    rateLimitPerUser: 0,
    lastPinTimestamp: 1594845448699,
    guild: Guild {
      members: [GuildMemberManager],
      channels: [GuildChannelManager],
      roles: [RoleManager],
      presences: [PresenceManager],
      voiceStates: [VoiceStateManager],
      deleted: false,
      available: true,
      id: '729663937557168181',
      shardID: 0,
      name: 'Bot Testing',
      icon: null,
      splash: null,
      region: 'europe',
      memberCount: 6,
      large: false,
      features: [],
      applicationID: null,
      afkTimeout: 300,
      afkChannelID: null,
      systemChannelID: '729663938148565134',
      embedEnabled: undefined,
      premiumTier: 0,
      premiumSubscriptionCount: 0,
      verificationLevel: 'MEDIUM',
      explicitContentFilter: 'DISABLED',
      mfaLevel: 0,
      joinedTimestamp: 1594035922599,
      defaultMessageNotifications: 'ALL',
      systemChannelFlags: [SystemChannelFlags],
      vanityURLCode: null,
      description: null,
      banner: null,
      rulesChannelID: null,
      publicUpdatesChannelID: null,
      ownerID: '402159567200583680',
      emojis: [GuildEmojiManager]
    },
    messages: MessageManager {
      cacheType: [Function: LimitedCollection],
      cache: [LimitedCollection [Map]],
      channel: [Circular]
    },
    _typing: Map { '402159567200583680' => [Object] }
  },
  deleted: false,
  id: '733951355240316948',
  type: 'DEFAULT',
  content: '>giveRole <@!655895332722442282> Mod',
  author: User {
    id: '402159567200583680',
    bot: false,
    username: '🎁🎄KFM🎄🎁',
    discriminator: '3147',
    avatar: '5f322e4ab3ec14e9fa83e1c77d8bb36d',
    lastMessageID: '733951355240316948',
    lastMessageChannelID: '729663938148565134',
    flags: UserFlags { bitfield: 0 }
  },
  pinned: false,
  tts: false,
  nonce: '733951341017169920',
  system: false,
  embeds: [],
  attachments: Collection [Map] {},
  createdTimestamp: 1595058039246,
  editedTimestamp: null,
  reactions: ReactionManager {
    cacheType: [Function: Collection],
    cache: Collection [Map] {},
    message: [Circular]
  },
  mentions: MessageMentions {
    everyone: false,
    users: Collection [Map] { '655895332722442282' => [User] },
    roles: Collection [Map] {},
    _members: Collection [Map] { '655895332722442282' => [GuildMember] },
    _channels: null,
    crosspostedChannels: Collection [Map] {}
  },
  webhookID: null,
  application: null,
  activity: null,
  _edits: [],
  flags: MessageFlags { bitfield: 0 },
  reference: null
}
roletrue
Error: undefined

Thanks in advance!提前致谢!

EDIT: It looks like the role.id is undefined.编辑:看起来 role.id 是未定义的。 I will try to see how to fix that.我会尝试看看如何解决这个问题。

I think problem with this part of code我认为这部分代码有问题

    console.log("role" + role); // true
    Member.roles.add(role.id).catch(e => console.log("Error: " + e.message));

You trying add undefined in roles, because:您尝试在角色中添加undefined ,因为:

 const roleId = (true).id; console.log(roleId); //undefined

Your got your Boolean from some function which always return true or false , try change this to find你从一些function 得到你的Boolean ,它总是返回truefalse ,尝试改变这个来找到

try add some id like 733951355240316948.尝试添加一些id ,例如 733951355240316948。

I dont know how looks role.id , but role must be Object我不知道role.id看起来如何,但角色必须是Object

for example:例如:

{
  id:'asdasdawe3534536456',
  // other fields
}

You need find what expect .add function in Member.roles.add您需要在 Member.roles.add 中找到期望的.add Member.roles.add

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM