Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you set the Meteor.userId() for use within autoform/simple-schema?

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 :)

like image 704
user1532669 Avatar asked Aug 18 '14 18:08

user1532669


3 Answers

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/

like image 59
Kuba Wyrobek Avatar answered Sep 22 '22 03:09

Kuba Wyrobek


This worked for me:

creatorID: {
    type: String,
    regEx: SimpleSchema.RegEx.Id,
    autoform: {
        type: "hidden",
        label: false
    },
    autoValue: function () { return Meteor.userId() },
}
like image 34
Sabrina Leggett Avatar answered Sep 20 '22 03:09

Sabrina Leggett


this.userId() didn't work for me, instead this did:

    autoValue: function() {
        return Meteor.user()._id;
    }
like image 37
Ronin Avatar answered Sep 20 '22 03:09

Ronin