Loop through Snowflake array

时光怂恿深爱的人放手 提交于 2019-12-07 07:41:11

问题


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.


回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/43233040/loop-through-snowflake-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!