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

\testing-forbidden\node_modules\@sapphire\framework\dist\lib\utils\logger\Logger.js:27
this.write(ILogger_js.LogLevel.Error, ...values);
^

TypeError: Cannot read properties of undefined (reading 'write')
at error (\testing-forbidden\node_modules\@sapphire\framework\dist\lib\utils\logger\Logger.js:27:10)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Node.js v18.14.1
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
\testing-forbidden\node_modules\@sapphire\framework\dist\lib\utils\logger\Logger.js:27
this.write(ILogger_js.LogLevel.Error, ...values);
^

TypeError: Cannot read properties of undefined (reading 'write')
at error (\testing-forbidden\node_modules\@sapphire\framework\dist\lib\utils\logger\Logger.js:27:10)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Node.js v18.14.1
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
I am using @sapphire/plugin-logger. I am building a provider to my database that currently uses the logging methods, I assumed it was because the client hadn't logged in yet so I moved my provider instantiation to the ReadyListener. This is my ReadyListener at the moment
import { Listener } from "@sapphire/framework";
import type { Client } from "discord.js";

import { MongoProvider } from "../provider/MongoProvider";
import * as Schemas from "../provider/schemas";

export class ReadyListener extends Listener {
public constructor(context: Listener.Context, options: Listener.Options) {
super(context, {
...options,
event: "ready",
once: true,
});
}

public run(bot: Client) {
if (!bot.user) {
this.container.logger.error(
"Ready: Bot has initialised with no user"
);
return;
}
this.container.logger.info(
`Ready: Bot has initialised as ${bot.user.tag} (${bot.user.id})`
);

// Setup theprovider
bot.provider = new MongoProvider(
{
hostname: process.env.DB_HOSTNAME,
port: process.env.DB_PORT,
auth: {
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
},
},
{ guilds: Schemas.GuildSchema }
);

// Test the that the insert document method works
bot.provider.waitForConnection(() => {
bot.provider.insertDocument("guilds", {
guildID: "1",
prefix: "!",
});
});
}
}
import { Listener } from "@sapphire/framework";
import type { Client } from "discord.js";

import { MongoProvider } from "../provider/MongoProvider";
import * as Schemas from "../provider/schemas";

export class ReadyListener extends Listener {
public constructor(context: Listener.Context, options: Listener.Options) {
super(context, {
...options,
event: "ready",
once: true,
});
}

public run(bot: Client) {
if (!bot.user) {
this.container.logger.error(
"Ready: Bot has initialised with no user"
);
return;
}
this.container.logger.info(
`Ready: Bot has initialised as ${bot.user.tag} (${bot.user.id})`
);

// Setup theprovider
bot.provider = new MongoProvider(
{
hostname: process.env.DB_HOSTNAME,
port: process.env.DB_PORT,
auth: {
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
},
},
{ guilds: Schemas.GuildSchema }
);

// Test the that the insert document method works
bot.provider.waitForConnection(() => {
bot.provider.insertDocument("guilds", {
guildID: "1",
prefix: "!",
});
});
}
}
I can also confirm that the logger is functioning before the error
2023-04-07 22:53:02 - INFO - ApplicationCommandRegistries: Initializing...
2023-04-07 22:53:02 - INFO - Ready: Bot has initialised as testing-forbidden#5339 (1091541627056689242)
2023-04-07 22:53:02 - INFO - MongoProvider: Attempting to connect to the database...
2023-04-07 22:53:02 - INFO - MongoProvider: Connected to database. Initialising provider...
2023-04-07 22:53:02 - INFO - MongoProvider: Provider initialised.
2023-04-07 22:53:02 - INFO - ApplicationCommandRegistries: Initializing...
2023-04-07 22:53:02 - INFO - Ready: Bot has initialised as testing-forbidden#5339 (1091541627056689242)
2023-04-07 22:53:02 - INFO - MongoProvider: Attempting to connect to the database...
2023-04-07 22:53:02 - INFO - MongoProvider: Connected to database. Initialising provider...
2023-04-07 22:53:02 - INFO - MongoProvider: Provider initialised.
Solution:
Actually I figured it out, ```ts const result = await model.create(document).catch((error) => { container.logger.error(error); });...
Jump to solution
6 Replies
Duck
Duck15mo ago
I am thinking, maybe when it runs the insertDocument method, if it fails it might crash the logger Though it's annoying no other error appears before the logger so I don't know
Solution
Duck
Duck15mo ago
Actually I figured it out,
const result = await model.create(document).catch((error) => {
container.logger.error(error);
});
const result = await model.create(document).catch((error) => {
container.logger.error(error);
});
Doing .catch(container.logger.error) will just absolutely obliterate the logger lmao
MRDGH2821
MRDGH282114mo ago
Do you separately import container or use this.container ? Or you create a new instance of logger? Because I tried all three and it still throws me such error
Duck
Duck14mo ago
I import the container in this instance, what are you doing specifically?
MRDGH2821
MRDGH282114mo ago
I did: 1. Use this.container.logger 2.
import {Logger} from '@sapphire/plugin-logger'

const customLogger = new Logger()
import {Logger} from '@sapphire/plugin-logger'

const customLogger = new Logger()
And had been importing this everywhere. Both of them crashed whenever there was error and the logger was invoked/called 3. Now I use:
import {container} from '@sapphire/framework'

//Or

import {container} from '@sapphire/pieces'

const logger = container.logger

//Use logger
import {container} from '@sapphire/framework'

//Or

import {container} from '@sapphire/pieces'

const logger = container.logger

//Use logger
Which is working so far without logger itself crashing in any way
Duck
Duck14mo ago
Yea you are supposed to import it from framework and not the plugin itself