TypeError: Cannot read property 'send' of undefined

Please explain why I am getting this error:
TypeError: Cannot read property ‘send’ of undefined

// server.js
const Discord = require(‘discord.js’);
const client = new Discord.Client();

client.on(‘ready’, () => {
const something = require(’./folder/file.js’);
setInterval(something.word, 10000);

// env.general - discord channel id
const general = client.channels.get(env.general);
if (!general) return;
module.exports = { general };
});

// file.js
function word() {
const channel = require(’…/server.js’);
channel.general.send(‘ok’);
}
module.exports = { word };

// node v 10.x

Hi @AndriyMeleshko – have you verified that general is defined in server.js (and that it’s not short-circuiting with that if (!general) return? I’d be happy to take a look at your code if the project is public

Its because of the load / compile order of the require functions.

Edit - see https://nodejs.org/api/modules.html#modules_cycles

A better way to do it is to have only file.js as a module, and pass it parameters, like …

setInterval(() => something.word(general), 10000);

Hey, can you send me an invite code to the project so I can see what server.js is cause you never really showed it. And, is there more?

Use process.env.general instead of env.general

3 Likes

Hello @chroventer
Thank you for your advice.

const general = client.channels.get(process.env.general);
// or
const env = process.env;
const general = client.channels.get(env.general);

Hello @chessebuilderman
Thank you for your interest.


// server.js
Lines 25 and 26 are hidden to stop the error.

Hello @mishavee
Thank you for your advice.
I’ll try to do.

Hello @househaunt
Thank you for your interest.


// server.js
Lines 25 and 26 are hidden to stop the error.

I think the project may be private – when I followed the link I got the error that there was no project there. I’d be happy to take a look if you send me an invite to join the project!

@househaunt
Sorry

No worries! Are you still getting the same error that you were before? I noticed after remixing that the package name in the package.json is throwing an invalid name error, I believe due to the í character. If you’re seeing the same error, that might at least partially resolve any issues you’re seeing!

I corrected the name, there is still an error.

What error is it? The same one as before?

Hello @SpeedyCraftah
Yes
TypeError: Cannot read property ‘send’ of undefined
Jump Toat fetch.then.then (/app/stream/stream.js:67:33)

There are circular require() calls, probably causing an incomplete loading of server.js, and channel.general being undefined.

server.js …

const command = require(`./stream/${file}`);

stream.js …

const channel = require('../server.js');
1 Like

Hello @mishavee
Thank you for your advice.
You are probably right.

Hello @mishavee Thank you very much!
It helped me:
discord/stream/stream.js
discord/stream/table.js
discord/stream/commands/del.js
discord/stream/commands/list.js

// server.js
Client.commandsDiscordp = new Discord.Collection();
const commandFilesStream = fs.readdirSync(’./discord/stream/commands’).filter(file => file.endsWith(’.js’));
for (const file of commandFilesStream) {
const command = require(./discord/stream/commands/${file});
Client.commandsDiscordp.set(command.name, command);
}

1 Like

I’ve have changed the category of this topic to ‘Discord Help’

1 Like