How can I update the values of an embedded object in a mongoDB document?
{{service.id}} and {{service.username}} show the correct values in the table template, but I am not sure how to call them in the saveItem()
function. When I try service.id
, service.$.id
, and service.[0].id
, I get Error: Unexpected token .
. When I try "service.id"
, the form submits and nothing happens, when I try "service.[0].id"
, the form gets stuck on edit and nothing happens, and when I try "service.$.id"
, I get an error saying the $ field cannot be updated.
Should I have something else in my javascript code? Or am I doing something wrong when defining the schema (ie don't need the dollar signs).
Thanks !
Here is my code:
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>
Got to the answer with the help of @MichelFloyd.
Schema should be as follows: (no $ signs)
"service.$": {
type: [Object]
},
"service.$.userid": {
type: Number
},
"service.$.username": {
type: String
}
And then this will work:
var editItem = {
_id: $('#editId').val(),
name: $('#editName').val(),
"service.userid": $('#editServiceId').val(),
"service.username": $('#editServiceUsername').val()
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.