Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can not read properties of null (reading 'username')

I’m trying to find a user by id but its erroring.

Here is my code:

module.exports = {
  name: 'whois',
  execute: (client, message, args) => {
    const userId = args[0];
    const member = message.guild.members.fetch(userId);
    message.reply(`Thats ${member.user.username}!`);
  }
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The cause of the error is due to the fact you’re not awaiting the fetch returned from <GuildMemberManager>#fetch.

Change your code to the following:

module.exports = {
  name: 'whois',
  execute: async (client, message, args) => {
    const userId = args[0];
    const member = await message.guild.members.fetch(userId);
    await message.reply(`Thats ${member.user.username}!`);
  }
}

documentation

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading