如何验证和更新mongoDB Collection文档中的嵌入式对象
How to Validate and Update an embedded object in a document of a mongoDB Collection
如何更新mongoDB文档中嵌入对象的值?
{{服务。Id}}和{{服务。在表模板中显示正确的值,但我不确定如何在saveItem()
函数中调用它们。当我尝试service.id
, service.$.id
和service.[0].id
时,我得到Error: Unexpected token .
。当我尝试"service.id"
,表单提交,什么都没有发生,当我尝试"service.[0].id"
,表单卡住编辑,什么都没有发生,当我尝试"service.$.id"
,我得到一个错误说$字段无法更新。
我应该有别的东西在我的javascript代码?或者我在定义模式时做错了什么(即不需要美元符号)。
谢谢!下面是我的代码:
var Schemas = {};
Items = new Meteor.Collection('items');
Schemas.Items = new SimpleSchema({
_id: {
type: String,
},
name: {
type: String,
label: "Item Name",
min: 1
},
"service.$": {
type: [Object]
},
"service.$.id": {
type: Number
},
"service.$.username": {
type: String
}
});
Items.attachSchema(Schemas.Items);
var saveItem = function() {
var editItem = {
_id: $('#editId').val(),
name: $('#editName').val(),
"service.$.id": $('#editServiceId').val(), //not working
"service.$.username": $('#editServiceUsername').val() //not working
}
Items.update(Session.get('editItemId'), {$set: editItem}, {validationContext: 'updateForm'}, function(error, result) {
if(!error){
Session.set('editItemId', null);
}
});
}
Template._editItemsItem.helpers({
editing: function() {
return Session.equals("editItemId", this._id);
}
});
Template._editItemsItem.events({
'click .editItem': function() {
Items.simpleSchema().namedContext('updateForm').resetValidation();
Items.simpleSchema().namedContext('insertForm').resetValidation();
Session.set("editItemId", this._id);
},
'click .cancelItemEdit': function() {
Items.simpleSchema().namedContext('updateForm').resetValidation();
Items.simpleSchema().namedContext('insertForm').resetValidation();
Session.set("editItemId", null);
},
'click .saveItem': function() {
saveTeam();
},
'keypress input': function(e){
if(e.keyCode === 13){
saveItem();
}
else if(e.keyCode === 27){
Items.simpleSchema().namedContext('updateForm').resetValidation();
Items.simpleSchema().namedContext('insertForm').resetValidation();
Session.set("editItemId", null);
}
}
});
Template._editItems.helpers({
items: function() {
return Items.find();
},
});
<template name="_editItems">
<table class="ui very compact selectable celled table">
<thead>
<tr>
<th>_id</th>
<th>Name</th>
<th>Service Name</th>
<th>Service Id</th>
<th>Edit</th>
</tr>
</thead>
<tbody>
{{#each items}}
{{> _editItemsItem}}
{{/each}}
</tbody>
</table>
</template>
<template name="_editItemsItem">
{{#if editing}}
<tr class="ui form">
<td><div class="ui small input"><input type="text" id="editId" value="{{_id}}"></div></td>
<td><div class="ui small input"><input type="text" id="editName" value="{{name}}"></div></td>
<td><div class="ui small input"><input type="text" id="editServiceUsername" value="{{service.username}}"></div></td>
<td><div class="ui small input"><input type="text" id="editServiceId" value="{{service.id}}"></div></td>
<td>
<button class="saveItem ui small circular primary button "><i class="ui save icon"></i></button>
<button class="cancelItemEdit ui small circular red button "><i class="ui cancel icon"></i></button>
</td>
</tr>
{{else}}
<tr>
<td>{{_id}}</td>
<td>{{name}}</td>
<td>{{service.username}}</td>
<td>{{service.id}}</td>
<td>
<button class="editItem ui small circular button"><i class="ui edit icon"></i></button>
</td>
</tr>
{{/if}}
</template>
在@MichelFloyd的帮助下找到了答案。
模式应该如下所示:(无$符号)
"service.$": {
type: [Object]
},
"service.$.userid": {
type: Number
},
"service.$.username": {
type: String
}
然后这个就可以了:
var editItem = {
_id: $('#editId').val(),
name: $('#editName').val(),
"service.userid": $('#editServiceId').val(),
"service.username": $('#editServiceUsername').val()
}
相关文章:
- 使用promise和mongoose对文档进行排序
- document.open/document.write没有正确地清除chrome中的文档——这是chrome的错误吗
- Ajax请求文档就绪会导致jquery加载缓慢
- MongoDB (php) - 以数组而不是多个属性的形式返回文档属性
- 谷歌文档表面引擎
- 来自文档或下一个静态父级的事件委派
- 如何将childNodes用于XML文档
- 文档就绪提供了错误的选择器高度
- 将当前用户的 ID 推送到 meteorjs 中集合/文档的内部数组
- 未捕获的语法错误:无法在“文档”上执行“查询选择器”
- BrowserId登录请求在文档加载时被调用
- 从Javascript和Php变量创建Html模板文档
- 如何通过谷歌应用程序脚本从谷歌文档中的位置确定命名范围
- 在jquery文档准备好之前加载Modernizr
- 如何判断何时将dom节点添加到文档中
- 无法获取文档.GetElementById工作正常
- 为什么是文档.旧版应用程序中的DOM-object.properties为null
- 如何在新的Meteor Collection文档中设置服务器上的created_on字段
- 如何验证和更新mongoDB Collection文档中的嵌入式对象
- Backbone.js collection.remove文档说明