Search code examples
meteormeteor-autoform

Recording username with meteror autoform


I'm creating a form using cfs:autoform to capture a photo and caption from submitted on the client like this:

Photos = new Mongo.Collection("photos");
Photos.attachSchema(new SimpleSchema({
  userId:{
    type: String,
    autoValue:function(){return this.userId},

  },
  userName:{
      type: String,
      autoValue:function(){return Meteor.users.find({_id: this.userId}).username},
  },

  groupMembers: {
    type: String
  },
  comments: {
    type: String  
  },
  fileId: {
    type: String
  }
}));

I've gotten the code to successfully capture and fill in the userId, as well as the comment and uploaded photo, but I can't seem to get it to capture the username.


Solution

  • It's most likely because you're using find instead of findOne. Since find returns a cursor and not a single document you can't access the username value, because it's not a property of the cursor. If you change it to findOne it should work.

    userName:{
          type: String,
          autoValue:function(){return Meteor.users.findOne({_id: this.userId}).username},
      }