Dubellest
Dubellest

Reputation: 82

Why is my code receiving undefined when writing a file?

I am trying to log all messages in a file for the bot on my server. However, whenever I run my bot and someone sends a message, I get this error:

UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_CALLBACK]: Callback must be a function. Received undefined

Here is my code:

if (message.channel.type !== 'dm')
{
var log = `[SERVER MESSAGE] ${message.member.user.tag}: "${message.content}" in #${message.channel.name}`;
  console.log(log);
  fs.writeFile("C:/Users/dubwi/Desktop/Discord_Bot/chatlog.json", `${log}`)
}
else{
  message.reply("I don't accept DMs at the moment");
  var log = `[DM MESSAGE] ${message.author.tag}: "${message.content}"`;
  console.log(log);
  fs.writeFile("C:/Users/dubwi/Desktop/Discord_Bot/chatlog.json", `${log}`)
}

Upvotes: 2

Views: 1357

Answers (1)

covo
covo

Reputation: 540

You are using the async method (fs.writeFile -doc), which is expecting a callback function.

So you can either use the synchronous method (fs.writeFileSync -doc) which does not require a callback, or modify your code to work with callbacks

Hope this helps

Upvotes: 2

Related Questions