我正在通过以下一本书学习Meteor,现在我们想要insert()当前登录的用户的userId。
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);
}
}
},但是this.userId没有定义,所以insert()没有像预期的那样工作。还有什么能让这件事发挥作用?
不知何故,它在下面的代码中工作(定义了userId):
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);
}
});更新
为什么在服务器端,this.userId工作而不是Meteor.userId()?
Meteor.publish("Categories", function() {
return lists.find({owner:this.userId}, {fields:{Category:1}});
});发布于 2013-12-26 07:45:54
对于更新问题: Meteor.userId只能在方法调用中调用。在发布函数中使用this.userId。
https://stackoverflow.com/questions/17823268
复制相似问题