discord.js TypeError:无法读取属性';forEach&x27;未定义的
我有一些代码来获取特定频道中的邮件附件,并记录URL。这在一开始工作得很好,但现在我得到了TypeError“无法读取未定义的属性'forEach'discord.js TypeError:无法读取属性';forEach&x27;未定义的,discord.js,Discord.js,我有一些代码来获取特定频道中的邮件附件,并记录URL。这在一开始工作得很好,但现在我得到了TypeError“无法读取未定义的属性'forEach' messageAttachments.push(message.attachments); messageAttachments.forEach((element) => element.forEach(attachment_array =>console.log(attachment_array.url))); 我真的不知道为什么会
messageAttachments.push(message.attachments);
messageAttachments.forEach((element) => element.forEach(attachment_array =>console.log(attachment_array.url)));
我真的不知道为什么会发生这种情况,因为之前没有发生过这种情况,而且检查以确保元素已定义不起作用。似乎您推送到数组中的
消息
对象的附件
属性未定义…@derpirscher如果我执行类似If的操作(邮件类型。附件!==未定义)
,我仍然得到相同的错误。您使用forEach
两次。一次使用messageAttachments.forEach
,一次使用元素。forEach
messageAttachments
不能取消定义,因为推送会失败,您的代码不会到达下一行。因此,根据您向我们展示的代码,messageAttachments
中至少有一个元素
必须未定义。而messageAttachments
中唯一的元素
是message.attachments
。因此,这可能是未定义的,或者错误发生在其他地方,或者您正在其他地方添加未定义的元素……并且您的类型检查不正确。typeof
始终返回一个字符串。因此,您必须检查if(typeof message.attachments!=“undefined”)