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.1error 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.1error 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
@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
ReadyListener
.
This is my
ReadyListener
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,
const result = await model.create(document).catch((error) => { container.logger.error(error); });
const result = await model.create(document).catch((error) => { container.logger.error(error); });