Search code examples
discord.js

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

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