我的客户端文件中有以下事件:
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});
Session.set('adding_category', false);
}
}
},
...
});
这是相关的模板部分:
<template name="categories">
<div id="categories" class="btn-group">
{{#if new_cat}}
<div class="category">
<input type="text" id="add-category" value="" />
</div>
{{else}}
<div class="category btn btn-inverse" id="btnNewCat">+</div>
{{/if}}
{{#each lists}}
<div class="category btn {{list_status}}" id="{{_id}}">
{{Category}}
</div>
{{/each}}
</div>
</template>
因此,当插入新的类别时,应该设置所有者。但是没有。
这是MongoDB中的条目:
> db.lists.find()
{ "Category" : "test-admin", "_id" : "EsybjC3SLnNzCBx2t" }
知道我在做什么错吗? (实际上,我遵循的是“流星入门”这本书的借书库示例
编辑似乎:
console.log(this.userId);
undefined
最佳答案
交换此行:
lists.insert({Category:catVal,owner:this.userId});
对此:
lists.insert({Category:catVal,owner:Meteor.userId()});