简体   繁体   中英

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.

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

TypeError: Cannot read property 'user' of null

means your user variable is null which means test.members[u] is null. Try logging the test.members first and see if its filled.

user.user.username

is probably wrong. As it looks it should be just user.username

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM