![](/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.