简体   繁体   English

错误:TypeError:无法读取未定义的属性“forEach”

[英]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.

相关问题 错误类型错误:无法读取 Angular 中未定义的属性“forEach” - ERROR TypeError:Cannot read property 'forEach' of undefined in Angular 类型错误:无法读取未定义的属性“forEach” - TypeError: Cannot read property 'forEach' of undefined UnhandledPromiseRejectionWarning: TypeError: 无法读取未定义的属性“forEach” - UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'forEach' of undefined TypeError:无法读取未定义 React 的属性“forEach” - TypeError: Cannot read property 'forEach' of undefined React 类型错误:无法读取未定义 Javascript 的属性“forEach” - TypeError: Cannot read property 'forEach' of undefined Javascript 类型错误:无法读取未定义的属性“forEach”,javascript - TypeError: Cannot read property 'forEach' of undefined , javascript 未捕获的类型错误:无法读取未定义的属性“forEach”//检测未定义 - Uncaught TypeError: Cannot read property 'forEach' of undefined// detect undefined 无法读取未定义错误 JavaScript 的属性“forEach” - Cannot read property 'forEach' of undefined error JavaScript 错误:无法读取未定义的属性“ forEach” - Error : Cannot read property 'forEach' of undefined Angular 7 - 错误错误:未捕获(承诺):类型错误:无法读取未定义的属性“forEach” - Angular 7 - ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'forEach' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM