How to add a members field to a teams collection? Trying to add username from users collection to a new collection I created

Hi all,

Quick question. How do I add a new field to a collection? For example, I have a collection named Teams and I want to add a Members field to it. Right now Teams only takes text (the team I create and add to the collection) and a createdAt date. My goal is to be able to find a user through my Users collection and add them as a member to the Team collection.

I’m not even sure where to start. Right now this is what I have:

Template.teams.events({
   'submit .new-team'(event) {
      event.preventDefault()
      const target = event.target
      const text = target.text.value
      const members = target.members.value 
      newTeam.insert({
         text,
         createdAt: new Date(),
         members
      })
      (target.text.value = '')
   }
})

but I keep getting this error on my console: “TypeError: Cannot read property ‘value’ of undefined at Object.submit .new-team”

Any ideas on how to go about doing this? Would really appreciate the help. Thanks!

Securing schema mutations made in the client will not be easy. You will need to add carefully crafted allow and deny rules, or anyone will be able to use the browser console to make any changes they like. Those rules are notoriously difficult to get right. I strongly suggest using a Meteor Method for this. It is much easier to secure on the server.

Thanks for the reply! This project is actually for a junior dev interview I had last week.

I figured it probably makes more sense to add a Teams field to the Users collection instead. I tried something like this in my server, but alas, no luck.

const userTeam = {
   teamName: 'Team Bears & Beets'
}

Meteor.users.update(userId, {
   $set: {
      team: userTeam
   }
})

Meteor.publish('Meteor.users.userTeam', function({ userIds }) {
   const selector = { _id: { $in: userIds } }
   const options = {
      fields: { userTeam: 1 }
   }
   return Meteor.users.find(selector, options)
})

Any tips on how to add an extra custom field to the Users collection? I followed the “Custom data about users” from the Meteor docs but it was all a bit confusing.