UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“ get”

时间:2019-09-11 18:41:20

标签: javascript node.js discord.js

我的机器人日志中有此错误

-(node:315) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'get' of undefined

at guildInvites.find.i (/app/server.js:477:46)

    at Map.find (/rbd/pnpm-volume/e510aebc-aa8d-41e6-bdbe-632fceb24fc5/node_modules/.registry.npmjs.org/discord.js/11.5.1/node_modules/discord.js/src/util/Collection.js:506:11)

at member.guild.fetchInvites.then.guildInvites (/app/server.js:477:33)

    at <anonymous>

    at process._tickCallback (internal/process/next_tick.js:189:7)

(node:315) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 355)

这是代码

const invites = {};
const wait = require('util').promisify(setTimeout);
client.on('ready', () => {
  wait(1000);
  client.guilds.forEach(g => {
    g.fetchInvites().then(guildInvites => {
      invites[g.id] = guildInvites;
    });
  });
});

 member.guild.fetchInvites().then(guildInvites => {
    const ei = invites[member.guild.id];
    invites[member.guild.id] = guildInvites;
    const invite = guildInvites.find(i => ei.get(i.code).uses < i.uses);
    const inviter = client.users.get(invite.inviter.id);
    if(!inviter) inviter = `I can\'t figure out how ${member.displayName} joined the server.`;

我不知道该怎么解决,这不是全部代码,我只是发送了一部分代码,但这是错误所在

请问有人可以告诉我我该如何处理Promise bc 例如,这是一个代码,我该如何处理

client.on("message", async message=>{
  if (message.isMentioned(client.user)){
let msg = await message.channel.send("xxx")
msg.edit(`xxx`)
}
});

1 个答案:

答案 0 :(得分:-1)

添加到新服务器后,当前代码将不会为其设置invites对象。如果用户加入并且在添加机器人后仍未​​重启,则eiinvites[member.guild.id])尚未定义。这是错误的根源。

要解决此问题,请将侦听器附加到客户端的guildCreate事件,该事件在invites中定义了公会的属性。

client.on('guildCreate', async guild => invites[guild.id] = await guild.fetchInvites());