我正在尝试在更新集合后运行某些插入语句.例如,如果用户将嵌入文档位置添加到其用户文档中,我还希望将该嵌入文档插入到单独的位置集合中.有没有办法在服务器端执行此操作,以确保操作运行?
我正在通过阅读一本书来学习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);
}
}
},
Run Code Online (Sandbox Code Playgroud)
但是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);
}
});
Run Code Online (Sandbox Code Playgroud)
为什么在服务器端,this.userId工作但不是Meteor.userId()?
Meteor.publish("Categories", function() {
return lists.find({owner:this.userId}, {fields:{Category:1}});
});
Run Code Online (Sandbox Code Playgroud)