如何验证和更新mongoDB集合的文档中的嵌入对象

时间:2015-08-25 18:13:28

标签: javascript mongodb meteor

如何更新mongoDB文档中嵌入对象的值?

{{service.id}}和{{service.username}}在表格模板中显示正确的值,但我不确定如何在saveItem()函数中调用它们。当我尝试service.idservice.$.idservice.[0].id时,我得到Error: Unexpected token .。当我尝试"service.id"时,表单提交并且没有任何反应,当我尝试"service.[0].id"时,表单会在编辑时停滞不前,没有任何反应,当我尝试"service.$.id"时,我会收到错误消息$ field无法更新。

我的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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

在@MichelFloyd的帮助下得到答案。

架构应如下:(无$ sign)

 "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()
}