led
led

Reputation: 361

Get users ID from name - best practice

I have a select dropdown with a list of users first names from the Meteor.users collection. I want to add the selected users ID as a property of another collection when submitting the form. What is the best way to get that users ID?

  1. Use a Meteor.method and search users with the name string to get ID and assign to insert object
  2. Attached the ID as an attribute to the tag when iteration over the collection and retrieve when submitting form.

Is there an easier way?

Upvotes: 0

Views: 65

Answers (1)

Nix
Nix

Reputation: 58542

Code is useful to help clarify. But if I understand correctly the way I would have done it was to populate the dropdown with id / name. So the <option> display would be name, but the value that is selected would be _id.

<select class="" id="user_select" >  
  {{#each my_users }}
    <option value="{{_id}}">{{userName}}</option>
  {{/each}}
</select>

my_users would return objects like [{_id: xxxxx, userName: "nix" }, { ..} ]

Upvotes: 1

Related Questions