I'm brand new to coding, but decided to give it a shot just to make a fun little bot for a Discord server to surprise my friends, but I'm having a bit of an issue with refactoring in Visual Code Studio - for some reason, my forEach code isn't working all of a sudden, and I can't seem to figure out why or how to fix it,
here's the error I'm getting whenever I try to run it
files.forEach(file => {
^
TypeError: Cannot read property 'forEach' of undefined
and here's the section of code I'm using
fs.readdir("./events/", (err, files) => {
files.forEach(file => {
const eventHandler = require('./events/message.js')
const eventmessage = file.split(".")[0]
client.on(eventmessage, (...args) => eventHandler(client,...args))
})
Like I said, I'm new at coding, and have largely been following online guides, but I've tried a few things, such as removing it, restarting the program, rewriting it in a different window to see that does anything, reinstalling what I'm using in the code, and so on
I'm probably being an absolute idiot and missing some piece but I'm totally baffled so any guidance (or a different way to refactor!) would help!
Thank you in advance!
Note: I am largely following this guide if that helps any!
https://thomlom.dev/create-a-discord-bot-under-15-minutes/
EDIT: Here's a coded copy of what I've been following, as far as I can see my code is basically exactly the same
https://github.com/thomlom/discord-bot-example
Maybe you forgot to create the events folder, because I got the same error when the folder doesn't exist.
Try to follow this part of the tutorial again
To modularize our code, we will create an events folder. This folder will contain .js files whose name will match the different events name discord.js listens to:
Create an events folder.
In this folder, create three files: ready.js, message.js and guildMemberAdd.js.
Related
Recently I started having Issues with the Google Tag Manager.
I can't track it to a Tag or a Trigger (activated and deactivated individually to check).
The error started occurring this week (no updates were made to the system or template). I noticed when testing the implementation of a new Tag in GTM. That shortly worked but then this error message started showing in the console. Removing the new Tag didn't change the situation and since the code worked for a while (some 30minutes) I don't expect a direct relationship between the new tag and the error.
All I have is the console in the frontend that shows the following error message:
Uncaught TypeError: Wb.set is not a function
The error stems from this file:
https://www.googletagmanager.com/gtm.js?id=GTM-XXXXXXX
Wb is started as a Map just a few steps earlier:
Wb=new Map(Vb.h.F);
Wb.set("style",{ya:4});
The GTM Script is copied and pasted from the GTM-Admin without any change made to it and is included in the header.php file of the Wordpress template. I tried copying it again and replacing the script that was in the header.php previously but the result was the same.
If looking at the actual error message in the console helps, the issue can be seen on the following URL:
https://www.bindella.ch
Any help in solving this or at least pointer to where to go look for errors would be very much appreciated.
Thanks
Very interesting, and yes, you're right, the error in the minified code makes no sense:
Let's try something different. First, just export your GTM container (the workspace that is synced with prod/live) as a JSON file in Admin section.
Open the file, look for something like set("style" See if you can find that Wb. Well, don't expect it to be Wb. I guess GTM minifies the code. It may be some weird chat bot, or something like that. From there, you'll be able to find the tag or variable that causes the issue.
I didn't look too deep into it, but that part of code looks sophisticated enough to be a part of the core container code. Actually, we can check it right here, on SO cuz it uses GTM too, let's do that.
Yes, SO has identical code. Looks like it's indeed core GTM. Well, there's a tiny probability that when you built the prod library last time, GTM had an issue and it deployed an artefact.
Try making a new workspace, make a nonsensical change in it and publish it to production. See if it lets you publish. Then see if that fixes the issue.
Now, if it doesn't then the next thing I'd try is trying to re-importing the exported JSON, forcing GTM compare the import to what's there and find differences.
Now if that shows no issues, then I would make a brand new container, load the config in it and try replacing the gtm loading script to see if the error still happens. It sounds like you can do that. If you can't, look a plugin like redirector to redirect the request to a different container.
Still the error with the new container? How about if you load a completely empty container? Got any errors?
There's a very slim chance that your front-end deploys one of a few vars GTM uses too, thus conflicting with your GTM. But it's very unlikely. Why would you use something like google_tag_manager or google_tag_data in the global scope...
If nothing helps and only the empty container doesn't give errors, then... well, then make a list of all tags firing along that error and start disabling them one by one. Or use breakpoints to narrow it down to the tag/library that causes it.
I refactored some code that passed on an object as a require to use it from a feature file and now I get the error on the title.
Previously:
const schema = require (´path to schema´)
let schemafile = [schema]
Now:
const schemafile = (fs.readFileSync(__dirname+'path to schema'+schema+.js)toString())
the above passed the contents of the joi schema file I prepared, but when i do:
joi.assert (responsebody, schemafile)
or
joi.attempt(responsebody, schemafile)
I get the error
TyperError: schema.validate is not a function
I do have the require for joi, and it seems everything passes fine and the error seems to be internal to joi, as the expression does exist there. What I can't make up is why it used to work before. Rolling back is not an option because I modified many, many files before I got this to hit that part of the code.
Sorry if the code is too vague but I cannot share much more (can't even copypaste from the VDI I use).
I use joi's latest version, I know joi.validate was deprecated and it is not used anywhere in my code.
Any help is appreciated.
Thanks!
PS: I am new to js, node and joi so sorry if the question is too basic.
I am writing a simple extension to open browser by clicking the extension button. I would like to know if there is a function which can execute passed shell command as argument. Also, it'd be really helpful if anyone can suggest a good simple reference for extension development.
From https://github.com/GNOME/gnome-shell/blob/master/js/misc/util.js:
// Runs #command_line in the background, handling any errors that
// occur when trying to parse or start the program.
function spawnCommandLine(command_line) {
try {
let [success, argv] = GLib.shell_parse_argv(command_line);
trySpawn(argv);
} catch (err) {
_handleSpawnError(command_line, err);
}
}
There are a few variations on that method in there. Save yourself mountains of headaches and just bookmark the GitHub repository.
Some quick links:
popupMenu.js: working with popup menus
panel.js: a good read for implementing "tray" icons
modalDialog.js: some UI elements were made to be reused, runDialog.js uses this for example
mpris.js: there are also good examples of using frameworks like DBus in gjs
I can't stress enough how much you'll get out of reading the gnome-shell source. Unfortunately, it's compiled into a resource file now so we don't have local copies to stumble upon.
UPDATE (2021)
If you're reading this, please instead see the documentation available on gjs.guide. Specifically the documentation on Spawning Subprocesses, which covers why this is a bad idea in extensions and how to do it slightly less bad.
If you're not interested in the result - i.e. when you want to open a browser window - you can just use GLib.spawn_command_line_async like so:
const GLib = imports.gi.GLib;
...
(this._menuEntries[i]).connect('activate', () => {
GLib.spawn_command_line_async('firefox http://example.com?p='+ my_params[i]);
});
If you need a synchronous result, read https://gjs.guide/guides/gio/subprocesses.html
Solution Found: Turns out the problem was a "circular" dependency. For anyone who else might encounter this problem, I found the answer here: Require returns an empty object
First time asking a question. I'm still learning, and I thought I knew what I was doing here, but I can't make sense of this.
I am working on a Node project with MongoDB/Mongoose. At the top of my file, I have:
const {Institution} = require('./institution');
const {Student} = require('./student');
When I run my program and use Institution.findOne() it errors and says cannot read property findOne of undefined. I use the Institution model in several other files and they all work fine.
The model is required in several steps before this one and always works fine. Since the model works in other cases, I would think the exports are working just fine.
const Institution = database.model('Institution', InstitutionSchema);
module.exports = {
Institution,
InstitutionSchema
};
Both institution.js and student.js are in the same folder as this file.
When I do console.log(Student), it returns the huge Mongoose object.
When I do console.log(Institution), it returns undefined.
If I console.log(Institution) from another module where it is working, it returns the Mongoose object.
The Institution collection is in my database and I can view the content in Robomongo viewer.
Am I missing something that I just can't see?
Thanks in advance for any help.
I tried to use socket.io in an simple app.
When it is calling the following js file:
node_modules\socket.io\lib\transports\websocket.js
The following line failed to load (It didn't really fail. It actually returned some empty result and failed some later codes using protocolVersions):
var protocolVersions = require('./websocket');
I debugged a little bit and it seems that the intention of this line is to load the following js file:
node_modules\socket.io\lib\transports\websocket\index.js
However because there is also a "websocket.js" (the file being run) under the "transports" folder, the require functions always tries to load "websocket.js" instead, and return some unexpected results.
I changed the folder name to "websockets" and call "require('./websockets')" instead, and it fixed the problem. However this is a shared library I am not feeling comfortable messing up its codes.
I am trying to understand why this problem happened this way and what should be the best way to work around it. I am sure this piece of codes has been well tested before released.
You probably already noticed that I am using backslash '\' to separate the path. Yes I am running it on Windows! Will that create any difference?
Thanks!