![](/img/trans.png)
[英]ERROR TypeError:Cannot read property 'forEach' of undefined in Angular
[英]Error : TypeError: Cannot read property 'forEach' of undefined
完整代码:
const Discord = require('discord.js');
const client = new Discord.Client({ partials: ["MESSAGE", "CHANNEL", "REACTION"]});
client.commands = new Discord.Collection()
client.events = new Discord.Collection()
['command', 'event'].forEach(handler =>{
require(`./handlers/${handler}`)(client, Discord)
});
client.login('tokenremoved')
这个错误实际上是由“自动分号注入”引起的。 如果您不在代码中放置分号,Javascript 将尽其所能将它们添加到引擎盖下。 它非常准确,但是,在某些情况下,例如这种情况,它会误解所写的内容。
// javascript sees this:
client.events = new Discord.Collection()
['command', 'event'].forEach(handler =>{
require(`./handlers/${handler}`)(client, Discord)
});
// and translates it to this:
client.events = new Discord.Collection()['command', 'event'].forEach(handler =>{
require(`./handlers/${handler}`)(client, Discord)
});
Javascript 认为您正在尝试使用括号访问Discord.Collection()
的参数。 但是,由于Collection#command
不存在,它返回未定义。 因此:
TypeError:无法读取未定义的属性“forEach”
const test = 'hello' [true, 123].forEach(() => 'this throws an error')
解决方法就是在Discord.Collection()
后面加一个分号
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.