I'm having trouble with my code while working through the Discover Meteor book and building a small app to deepen my understanding of the concepts:
Template.panelCM.events({
'click .editProductsCol': function(e) {
e.preventDefault();
if (confirm('Edit?')){
var currentProduct = this._id;
var productOptions = {
name: $(e.target).find('[name=productName]').val(),
description: $(e.target).find('[name=productDescription]').val()
};
Products.update(currentProduct, {$set: productOptions}, function(error) {
if (error) {
alert(error.reason);
throwError('Error');
} else {
Router.go('tabPage');
}
});
}},
'click .deleteProductsCol': function(e) {
e.preventDefault();
if (confirm("Delete?")) {
var currentProduct = this._id;
Products.remove(currentProduct);
Router.go('tabPage');
}
}});
The delete functionality is working fine, but I'm facing an issue with the update operation. After submitting, I receive the following error message:
MongoError: '$set' is empty. You must specify a field like so: {$mod: {<field>: ...}}
This is how my template looks:
<template name="panelCM">
{{#each products}}
<div class="col-xs-12 col-sm-6 col-md-6 mainCol">
<img src="../{{image}}"/>
<input type="text" name="productName" id="productName" class="form-control" placeholder="{{name}}">
<textarea name='productDescription' id="productDescription" class="form-control colP" rows="10"
placeholder="{{description}}" style="color: black"></textarea>
<button type="submit" class="btn btn-lg btn-success form-control editProductsCol">Edit</button>
<button type="submit" class="btn btn-lg btn-danger form-control deleteProductsCol">Delete</button>
</div>
{{/each}}</template>
I believe I may have misunderstood the purpose of the productOptions variable. It seems like I am creating an object that captures the values from specific HTML elements and passing it to the Products database for updating. I'm unsure if I need to also use an ID in my template, as seen in the book, since I can locate the correct element using the 'name' attribute (unsure about the technical term for this). Additionally, should the 'name' and 'description' fields in productOptions match the corresponding fields in my database?