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

Say that I want to display a list of user purchases after they log in. I use autoform and simple-schema to create form elements. When a user signs up for the first time, an empty shopping list form is displayed. When you submit the form, the shopping list is saved in db.

I want to know 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 this be done using autoform and simple-schema? Is it 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:)

+7
javascript meteor
source share
3 answers

If you are also using Collection2, 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 } } }); 

More details: https://github.com/aldeed/meteor-collection2/

+20
source share

This worked for me:

 creatorID: { type: String, regEx: SimpleSchema.RegEx.Id, autoform: { type: "hidden", label: false }, autoValue: function () { return Meteor.userId() }, } 
+4
source share

this.userId() did not work for me, instead it did:

  autoValue: function() { return Meteor.user()._id; } 
0
source share

All Articles