在Meteor.js中,为什么this.userId ==未定义? [英] In Meteor.js, Why is this.userId == undefined?
本文介绍了在Meteor.js中,为什么this.userId ==未定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在通过学习本书来学习Meteor,现在我们想insert()
当前登录用户的userId
.
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);
}
}
},
但是this.userId
未定义,因此insert()
未能按预期工作.要使它正常工作,缺少什么?
However this.userId
was undefined, so the insert()
didnt work as expected. What's missing to get this working?
它在下面的代码中以某种方式工作(定义了userId
):
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);
}
});
更新
为什么在服务器端this.userId
起作用但Meteor.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}});
});
推荐答案
您应该改用Meteor.userId()
.
这篇关于在Meteor.js中,为什么this.userId ==未定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文