使用.includes()时,Switch语句不起作用

client.on('chat', function(channel, userstate, message, self){ switch(message){ case message.includes(emotes[0]): numberOfEmotes[0]++; console.log("Emote0 has been used " + numberOfEmotes[0] + " time(s)"); break; case message.includes(emotes[1]): numberOfEmotes[1]++; console.log("Emote1 has been used " + numberOfEmotes[1] + " time(s)"); break; case message.includes(emotes[2]): numberOfEmotes[2]++; console.log("Emote2 has been used " + numberOfEmotes[2] + " time(s)"); break; case message.includes(emotes[3]): numberOfEmotes[3]++; console.log("Emote3 has been used " + numberOfEmotes[3] + " time(s)"); break; } /* if(message.includes(emotes[0])){ numberOfEmotes[0]++; console.log("Emote0 has been used " + numberOfEmotes[0] + " time(s)"); }*/ //console.log("** " + message + " **"); }); 

当函数chat.on被称为带有string的消息的variables应该通过switch语句运行时,我有一个数组与不同的string,如果消息包含该数组中的string,运行大小写。 但是什么也没有发生,这一切似乎正确这里可能是错误的?

开关不能按照你所期望的方式工作。 它将开关中写入的值与大小写块中的每个值进行比较。 所以在你的情况下,它会比较true / false与值的消息,它不会find相同的值,因此没有什么会发生。 你必须使用else语句或parsing消息,并排除Message'type / emote1'的值,然后将所有内容解压缩并放入交换机