Search code examples
javascriptnode.jsdiscorddiscord.js

Cant grap component of array within a for loop


I try to compare all user id's who have reacted to a message with another array. To do so I have fetched all users who have reacted and now I try to loop trough them, but I don't get the entries.

async function compare() {

//fetch reactions
    const channel = await interaction.client.channels.fetch(rewardchannel) 
    const message = await channel.messages.fetch(rewardpannel)
    const reaction = message.reactions.cache.get('✅')
    const users = await reaction.users.fetch()

//check users
    console.log(users) //1.

//loop through users
    for await (const user of users){
        console.log(user.id) //2.
    }
}

The 1. log should display the users array and work as it supposed to. The result is:

Collection(3) [Map] {
  '360053737638723584' => User {
    id: '360053737638723584',
    bot: false,
    system: false,
    flags: UserFlags { bitfield: 0 },
    username: 'Hell FIre',
    discriminator: '7210',
    avatar: '4ea6126903e68309b6dc2f7c22c8e30c',
    banner: undefined,
    accentColor: undefined
  },
  '964592219745050645' => User {
    id: '964592219745050645',
    bot: false,
    system: false,
    flags: UserFlags { bitfield: 0 },
    username: 'sola.eth',
    discriminator: '6722',
    avatar: 'bb3a9026ef8314e4f6be5228e1c955b0',
    banner: undefined,
    accentColor: undefined
  },
  '973154369233096717' => ClientUser {
    id: '973154369233096717',
    bot: true,
    system: false,
    flags: UserFlags { bitfield: 0 },
    username: 'Retweet Rewards',
    discriminator: '7150',
    avatar: null,
    banner: undefined,
    accentColor: undefined,
    verified: true,
    mfaEnabled: true
  }
}

However the 2. loop only returns undefined 3 times. So it is correctly looping through but it dont grap the acctual id of a user. Where is my mistake ?


Solution

  • Mapping the array of objects will go through each object.

    // log users
    users.map(user => {
      console.log(user.id);
    })