Skip to content
Advertisement

TypeError: Cannot read property ‘delete’ of undefined

I am getting an error (Cannot read property 'delete' of undefined). It’s probably an easy fix but I can’t seem to figure out how to fix it.

The error:

message.delete({timeout: 1000})

TypeError: Cannot read property 'delete' of undefined

My code:

const client = new Client();

let count = 0;
let timeout;

client.on('message', ({ channel, content, member, message }) => {
  if (channel.id === '933939208102494270') {
    if (member.user.bot) return;

    if (Number(content) === count + 1) {
      count++;

      if (timeout) clearTimeout(timeout);

      timeout = setTimeout(
        () => channel.send(++count).catch(console.error),

        100
      );
    } else if (member.id !== client.user.id) {
      message.delete({
        timeout: 1000,
      });
      channel.send(`${member} messed up!`).catch(console.error);
      message.delete({
        timeout: 1000,
      });

      //      count = 0

      if (timeout) clearTimeout(timeout);
    }
  }
});

Advertisement

Answer

The problem is that you try to destructure the first parameter of the callback, which is a Message object, and a Message doesn’t have a message property. If you need the message, you can destructure it inside the function.

So instead of

client.on('message', ({ channel, content, member, message }) => {
  // ...

it should be

client.on('message', (message) => {
  let { channel, content, member } = message
  // ...

This way you can use any other property and method of the message.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement