Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop through Snowflake array

I'm looking for a way to get data from this Collection.

The data looks like:

  '0000000' => GuildMember {
  guild:
   Guild {
     members: [Object],
     id: '000000',
     name: 'Zombie',
     _rawVoiceStates: [Object] },
  user:
   User {
     id: '0000000',
     username: 'Orc',
  _roles: [ '0000' ],
  nickname: 'Orc',
  joinedTimestamp: 00000,
  lastMessageID: null },

  '0000000' => GuildMember {
  guild:
   Guild {
     members: [Object],
     id: '000000',
     name: 'Zombie',
     _rawVoiceStates: [Object] },
  user:
   User {
     id: '0000001',
     username: 'Orc1',
  _roles: [ '0000' ],
  nickname: 'Orc',
  joinedTimestamp: 00000,
  lastMessageID: null },
  _array: null,
  _keyArray: null }

My current loop is:

var user;
for(var u in test.members){
   user = test.members[u];
    console.log("["+u+"] "+user.username);
}

It currently kicks back a TypeError: Cannot read property 'user' of null

I originally thought this the data was an array, but it's not according to the Discord.js docs, but I'm still not sure how to pull the username data from the collection.

Any help would be helpful.

like image 978
Carl Avatar asked Apr 05 '17 13:04

Carl


1 Answers

Now i looked into the discord.js API and i think what u got todo is something like this (assuming test is your guild object):

test.members.forEach(function(guildMember, guildMemberId) {
   console.log(guildMemberId, guildMember.user.username);
})

If that doesn't work try along the lines of:

var membersArray = test.members.array();

for(var guildMemberId in membersArray) {
   console.log(guildMemberId, membersArray[guildMemberId].user.username);
}
like image 151
xDreamCoding Avatar answered Oct 14 '22 06:10

xDreamCoding