I'm learning Meteor by following a book, and right now we want to insert()
the userId
of the user that is currently logged in.
Template.categories.events({
'keyup #add-category': function(e, t) {
if(e.which == 13) {
var catVal = String(e.target.value || "");
if(catVal) {
lists.insert({Category: catVal, owner: this.userId});
console.log(this.userId);
Session.set('adding_category',false);
}
}
},
However this.userId
was undefined, so the insert()
didnt work as expected. What's missing to get this working?
Somehow it works in the code below (userId
is defined):
lists.allow({
insert: function(userId, doc) {
return adminUser(userId);
},
update: function(userId, docs, fields, modifier) {
return adminUser(userId);
},
remove: function(userId, docs) {
return adminUser(userId);
}
});
Update
Why is it that on the server-side, this.userId
works but not Meteor.userId()
?
Meteor.publish("Categories", function() {
return lists.find({owner:this.userId}, {fields:{Category:1}});
});