How do you define Meteor.userId () for use in autoform / simple-schema?

advertisements

Say I want to display a list a user's shopping items once they are logged in. I'm using autoform and simple-schema to generate the form elements. When the user logs in for the first time a blank shopping list form is displayed. When submitting the form the shopping list is saved in the db.

What I want to know is how I can save this data for each user.

Normally I would do something like this:

      ShoppingList.insert({
        name: item_name,
        price: 0,
        createdBy: Meteor.userId()
      });

How would I acheive this using autoform and simple-schema? Would it be possible to do something like this:

Schema.ShoppingList = new SimpleSchema({
item: {
    type: String,
    regEx: /^[a-zA-Z-]{2,25}$/
},
price: {
    type: Number,
    regEx: /^[0-9]{2,25}$/
},
createdBy: {
    type: String,
    value: Meteor.userId()
}
});

Thanks again :)


If you are using also Collection2 then use autoValue:

Schema.ShoppingList = new SimpleSchema({
item: {
    type: String,
    regEx: /^[a-zA-Z-]{2,25}$/
},
price: {
    type: Number,
    regEx: /^[0-9]{2,25}$/
},
createdBy: {
    type: String,
    autoValue:function(){ return this.userId }
}
});

Read more : https://github.com/aldeed/meteor-collection2/