简体   繁体   English

流星更新收集错误

[英]Meteor update collection error

I have no idea whats wrong with my code, im reading the book Discover Meteor and building a small app to learn those concepts better: 我不知道我的代码有什么问题,我正在阅读《发现流星》一书,并构建了一个小型应用程序以更好地学习这些概念:

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 part works well, its just the update that I don't figure out whats wrong.. It gives me this error after submitting: MongoError: '$set' is empty. You must specify a field like so: {$mod: {<field>: ...}} 删除部分效果很好,它只是我不知道出什么问题的更新。.提交后,出现此错误: MongoError: '$set' is empty. You must specify a field like so: {$mod: {<field>: ...}} MongoError: '$set' is empty. You must specify a field like so: {$mod: {<field>: ...}}

This is my template: 这是我的模板:

<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>

What am I doing wrong, I didn't quite understand that productOptions var I think, if I understood correctly im just creating an object that gets whatever I put on that html element and then I pass it to my Products db to update it, what I don't quite understand is if I need to use an id on my template aswell, I saw it on the Discover Meteor book, but it doesn't make sense since im finding the correct element with the 'name' thing (no idea what thats called).. Also the name and description Im using in the productOptions, should be the same as the one im using on my db right? 我做错了什么,我不太了解productOptions var,如果我理解正确的话,我只是创建了一个对象,该对象将获取放置在该html元素上的内容,然后将其传递给我的Products db进行更新,我不太了解是不是也需要在模板上使用id,我在“发现流星”书上看到了它,但这没有意义,因为我用“ name”东西找到了正确的元素(不知道那也是所谓的。)而且在productOptions中使用的im名称和描述应该与我在数据库上使用的im相同吗?

$.find looks to the descendants of the DOM element it's called on. $ .find查找被调用的DOM元素的后代。 You're calling it on the edit button, which has no children. 您在没有子项的“修改”按钮上调用它。 That means it finds nothing, and the call to .val() returns undefined . 这意味着它什么也没找到,对.val()的调用返回undefined MongoDB's complaining that you're setting both fields to undefined , which means pretty much nothing. MongoDB抱怨您将两个字段都设置为undefined ,这几乎没有任何意义。 Try something like this instead: 尝试这样的事情:

'click .editProductsCol': function(e, template) {
    e.preventDefault();
    if (confirm('Edit?')) {

    var productOptions = {
        name: template.$('[name=productName]').val(),
        description: template.$('[name=productDescription]').val()
    };

    Products.update(this._id, {$set: productOptions}, function(error) {
        if (error) {
            alert(error.reason);
            throwError('Error');
        } else {
            Router.go('tabPage');
        }
    });
  }
},

A Meteor primer 流星底漆

Let's go through the code. 让我们看一下代码。 Meteor binds this inside an event handler to your model, so this._id (that you assigned to the currentProduct variable) gets the id of the document you want to update. 流星结合this一个事件处理模型内,所以this._id (您分配给currentProduct变量)让你要更新的文档的ID。

So, now you now what document (MongoDB items are called documents ) to update. 现在,您现在要更新什么文档 (MongoDB项称为document )。 How to do it and what to set it to? 怎么做以及如何设置? Well, we want to update based on the form data. 好吧,我们要基于表单数据进行更新。 The second parameter passed to the event handlers is the template instance, which contains a $ property that queries the DOM only inside the template's context. 传递给事件处理程序的第二个参数是template实例,它包含一个$属性 ,该属性仅在模板上下文内查询DOM。 That's where we get our form values. 那就是我们获取表单值的地方。

Now, we just have to call the update method on the Meteor.collection and we're gold. 现在,我们只需要在Meteor.collection上调用update方法就可以了。 As you can see from the docs, it takes the _id as a selector, a modifier object and a callback. 从文档中可以看到,它以_id作为选择器,修饰符对象和回调。 Your modifier is the object with the $set property. 您的修饰符是具有$set属性的对象。 It tells MongoDB to keep the document as it is, just adding/replacing the fields we're indicating (instead of replacing the whole document). 它告诉MongoDB保留文档原样,只是添加/替换我们指示的字段(而不是替换整个文档)。

One more thing, if there's no specific reason to do otherwise, I suggest you replace the placeholder attribute on your input elements with value s. 还有一两件事,如果没有特别的理由不这样做,我建议你更换的placeholder与您的输入元素属性value秒。 The placeholder is just a visual cue, it means nothing to the form content itself. 占位符只是一个视觉提示,对于表单内容本身没有任何意义。

    <input type="text" name="productName" id="productName" class="form-control" value="{{name}}" placeholder="Enter the name">
    <textarea name='productDescription' id="productDescription" class="form-control colP" rows="10" value="{{description}}" style="color: black" placeholder="Enter a description"></textarea>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM