TypeError: Cannot read properties of undefined (reading 'client')

Full Error:
TypeError: Cannot read properties of undefined (reading 'client')
at Object.handleError (C:\Users\cmart\Desktop\Discord Bots\Sapphire\src\utils.js:129:36)
at WarningsCommand.messageRun (C:\Users\cmart\Desktop\Discord Bots\Sapphire\src\commands\Moderation\warnings.js:96:27)
TypeError: Cannot read properties of undefined (reading 'client')
at Object.handleError (C:\Users\cmart\Desktop\Discord Bots\Sapphire\src\utils.js:129:36)
at WarningsCommand.messageRun (C:\Users\cmart\Desktop\Discord Bots\Sapphire\src\commands\Moderation\warnings.js:96:27)
Code: (utils.js line 129 to 131)
const errorLogs = this.container.client.channels.cache.get(
config.channels.errorLog
);
const errorLogs = this.container.client.channels.cache.get(
config.channels.errorLog
);
6 Replies
Lioness100
Lioness100•16mo ago
Please show the full file.
-Carlos👑
-Carlos👑OP•16mo ago
whole file or only the specific function
Lioness100
Lioness100•16mo ago
The full file
-Carlos👑
-Carlos👑OP•16mo ago
since my code is closed source, I can send you the password of a pastebin
Lioness100
Lioness100•16mo ago
Ok Ok, that's what I thought. If you directly call a standalone function from within a class method, this inside the standalone function will not refer to the class instance. Instead, it will refer to undefined.
function standaloneFunction() {
console.log(this);
}

class MyClass {
myMethod() {
standaloneFunction(); // 'this' inside standaloneFunction will be undefined
}
}

const obj = new MyClass();
obj.myMethod();
function standaloneFunction() {
console.log(this);
}

class MyClass {
myMethod() {
standaloneFunction(); // 'this' inside standaloneFunction will be undefined
}
}

const obj = new MyClass();
obj.myMethod();
The easiest way is to import container from @sapphire/framework and use it directly instead of through this. The harder ways would be to input the container via parameter, or do handleError.call(this) in the method, which explicitly sets the value of this.
-Carlos👑
-Carlos👑OP•16mo ago
ohhh this is why I shouldn't code at night ty
Want results from more Discord servers?
Add your server