How do I access name property of object when using find()

Been trying to get a property from a user but keep getting stuck. Any help would be appreciated.
In the ‘removeConfirm’ variable I want the name property of a user. The selectedPlayer holds the _id
of the user and I’m trying to use that as a search basis. Kind of search using the supplied id and return the object so I can reference the name property.


‘click .remove’: function(){
var selectedPlayer = Session.get(‘selectedPlayer’);

     var playerName = PlayersList.find(selectedPlayer).fetch();
     var removeConfirm = confirm('Whoa! hang on, do you really want to delete ' +  playerName.name);
     console.log(playerName);
     if(removeConfirm == true){
        PlayersList.remove(selectedPlayer);
     }

     
  }

var playerName = PlayersList.findOne({ _id: selectedPlayer }).name
console.log(playerName);

This returns an array:

var pList = PlayersList.find(playerId).fetch();

so you can get the name as:

if (pList[0]) console.log(pList[0].name);

You can also get just the one player:

var p = PlayersList.findOne(playerId);
if (p) console.log(p.name);

findOne() is like find().fetch()[0]

Thank you so much, I knew I was close, just didn’t know about the findOne()

Thanks for all your help, much appreciated :smile:

1 Like