Home > Software design >  TypeError: Cannot read property 'delete' of undefined
TypeError: Cannot read property 'delete' of undefined

Time:01-21

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);
    }
  }
});

CodePudding user response:

of undefined

look left of this... so message is undefined, log what that is at the top of client.on, will find that message is undefined and thus delete does not exist.

CodePudding user response:

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.

  •  Tags:  
  • Related