[英]Error : TypeError: Cannot read property 'forEach' of undefined
Complete code:完整代码:
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')
This error is actually caused by "automatic semicolon injection".这个错误实际上是由“自动分号注入”引起的。 If you don't put semicolons in your code, Javascript will add them under the hood to the best of its ability.
如果您不在代码中放置分号,Javascript 将尽其所能将它们添加到引擎盖下。 It's very accurate, however, there are a few instances such as this one where it misunderstands what's written.
它非常准确,但是,在某些情况下,例如这种情况,它会误解所写的内容。
// 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 thinks you're trying to access a parameter off Discord.Collection()
with the brackets. Javascript 认为您正在尝试使用括号访问
Discord.Collection()
的参数。 However, since Collection#command
doesn't exist, it returns undefined.但是,由于
Collection#command
不存在,它返回未定义。 Thus:因此:
TypeError: Cannot read property 'forEach' of undefined
TypeError:无法读取未定义的属性“forEach”
const test = 'hello' [true, 123].forEach(() => 'this throws an error')
The solution is just to add a semicolon after Discord.Collection()
解决方法就是在
Discord.Collection()
后面加一个分号
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.