[英]How to Validate and Update an embedded object in a document of a mongoDB Collection
如何更新mongoDB文檔中嵌入式對象的值?
{{service.id}}和{{service.username}}在表模板中顯示正確的值,但是我不確定如何在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()
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.