这一点.提交后未插入MongoDB属性
this.userId is undefined MongoDB property not inserted after submit
我的客户端文件中有以下事件:
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>
所以当插入一个新的Category时,应该设置owner ..但它没有。
MongoDB中的条目:
> db.lists.find()
{ "Category" : "test-admin", "_id" : "EsybjC3SLnNzCBx2t" }
知道我做错了什么吗?(实际上,我正在遵循"Getting Started with Meteor"图书借阅库示例
编辑似乎:
console.log(this.userId);
undefined
换行:
lists.insert({Category:catVal,owner:this.userId});
:
lists.insert({Category:catVal,owner:Meteor.userId()});
this
可能不是您在该事件中所期望的。您应该调试以确认情况是否如此。this.userId
可能没有定义。我建议将this
分配给这个事件处理程序函数之外的变量(称为"自我"或"那个"),但在this
将是您实际想要的范围内。然后可以在事件处理程序中引用该变量。
应该是这样的:
function thatRegistersEvents() {
var self = this;
// ...
registerSomeEvent(function () {
return self.someThisProperty;
});
}
这是正确的行为。如果您阅读官方的Meteor文档以获取this.UserId
的引用,那么它只在Meteor.publish()
和Meteor.methods()
中可用。this.UserId
在Meteor.template()
中不可用,这在上面的代码示例中已经完成了,因此必须在模板中使用Meteor.userId()
。
相关文章:
- MongoDB (php) - 以数组而不是多个属性的形式返回文档属性
- 更新属性工作不正常Meteor/MongoDB
- 如何在mongodb(mongose)中覆盖子文档的数组属性
- 获取从mongodb接收的对象的属性
- MongoDB属性迭代
- MEAN.js MongoDB查询中用户特定计算的属性
- 如何通过MongoDB驱动程序(node.js)将属性(属性名称是可变的)添加到对象中
- 如何使用环回和 mongodb 将嵌套对象中的属性强制转换为 ObjectId
- 如何使用猫鼬保存嵌套的mongoDB属性
- 使用从 MongoDB 获取的新数据更新对象属性
- MongoDB(通过Meteor)在更新集合文档时将对象属性视为字符串
- JS mongoDB按previousItem属性排序(父子关系)
- 在mongoDb中,如果字符串输入与任何数组元素匹配,如何获取整个记录(在这种情况下,数组是记录的属性)
- 如何在MongoDB MapReduce JavaScript查询中引用对象数组中对象的每个属性
- 如何在不使用 BsonIgnore 的情况下保存到 MongoDB 时跳过属性
- 在mongodb地理位置查询中使用对象属性作为$maxDistance参数
- Regex将JSON属性值包装在另一个字符串中.MongoDB转换
- MongoDB$sort聚合的Javascript属性顺序
- MongoDB多个排序属性:如何确定优先级
- 这一点.提交后未插入MongoDB属性