cancel
Showing results for 
Search instead for 
Did you mean: 

get Members of a Group and their Properties (userName), (Javascript)

testuser
Champ in-the-making
Champ in-the-making
Hello!

i want to get all members of a group and to show the userName of every Member. If i choose a specific member from the group (like the first one), it works fine. If i try to iterate over the group i get a type-Error "TypeError: Cannot read property "userName" from undefined ". What am i doing wrong?

var group = people.getGroup("GROUP_myGroup");

if(group){
    var members = people.getMembers(group);
    logger.log(members[0].properties.userName);     // works!
 
    for(i=0; i<= members.length; i++) {
       logger.log(members.properties.userName);  // error
    }
}

thanks for your help!
3 REPLIES 3

scouil
Star Contributor
Star Contributor
It probably is because your loop end condition is not strict hence you loop out of bounds.
Try with:

i < members.length

I personally like the foreach loop as well:

for(var member in members) {
    logger.log(member.properties.userName);
}

testuser
Champ in-the-making
Champ in-the-making
Thanks for your answer! It worked! 🙂

mehar460
Confirmed Champ
Confirmed Champ

Below will work perfectly fine.

var node = people.getGroup("GROUP_NS");

if(node){
var members = people.getMembers(node, true);
logger.log(members.length);
for(i=0; i< members.length; i++) {
logger.log(members[i].properties.userName); Output
}
}