Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
145
rated 0 times [  146] [ 1]  / answers: 1 / hits: 6557  / 2 Years ago, mon, april 4, 2022, 12:00:00

I'm really new to Javascript. So please bear with my garbage coding but I do not know what this error means and have spent really long trying to solve this. I am using Replit right now but I have also tried it on Visual Studio which gives the same error. Please help, I have no idea what to do!


Whenever I try to run my script it keeps erroring with this:


TypeError: Cannot read properties of undefined (reading 'forEach')
at Object.<anonymous> (/home/runner/myrepl/index.js:10:6)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:17:47

Here Is my Index.js:


var prefix = 'p!'

args.forEach((a, b) => {
args[b] = a.replace("`", "")

args[b] = args[b].replace(".", "")

args[b] = args[b].replace("`", "")
args[b] = args[b].replace(`"`, "")

args[b] = args[b].replace(`'`, "")

})
var args = Message.content.split(" ")
if (Message.author.bot == false) {
if (Message.content.startsWith("$")) {

if (Message.channel.id != CMDS && Message.author.id != DISCORD_ID) {
Message.reply("stop using cmds here idiot. <#" + CMDS + ">")
}
}

I have Node.js, discord.js, discord, and npm installed. My bot token is stored in token.json and my config (discord_ID and CMDS channel ID is stored in config.json).


I am trying to make it controlled from discord with a command like "p!". I don't know this problem at all.


More From » node.js

 Answers
1

You are trying to iterate through the args array (args.forEach()) before var args exists. If you move var args = Message.content.split(" ") just after var prefix = 'p!' (before your loop) then the issue presented should be solved. I couldn't say whether your code will then 'work', but it is why you are getting the error


[#222] Saturday, March 26, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mckaylab

Total Points: 311
Total Questions: 120
Total Answers: 93

Location: Montenegro
Member since Thu, Jun 16, 2022
2 Years ago
mckaylab questions
;