Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Meteor:更新文档时出错';带<;输入>;领域_Javascript_Mongodb_Meteor - Fatal编程技术网

Javascript Meteor:更新文档时出错';带<;输入>;领域

Javascript Meteor:更新文档时出错';带<;输入>;领域,javascript,mongodb,meteor,Javascript,Mongodb,Meteor,我在MongoDB文档上运行MeteorJS和onInput事件时遇到以下问题 在DB中,我有一个集合,其每个元素都包含一个数组。数组包含具有属性的对象,这些属性包括id和inputValue。我想在HTMLinput字段中显示每个对象的inputValue。我还希望当用户删除输入字段的所有内容时,数组中相应的对象被删除(通过onInput事件),数组的内容被重新显示 这在Meteor中应该很简单,下面是我正在使用的代码。但是,我遇到了以下问题: 当数组中的两个或多个对象最初具有相同的input

我在MongoDB文档上运行MeteorJS和onInput事件时遇到以下问题

在DB中,我有一个集合,其每个元素都包含一个数组。数组包含具有属性的对象,这些属性包括
id
inputValue
。我想在HTML
input
字段中显示每个对象的
inputValue
。我还希望当用户删除输入字段的所有内容时,数组中相应的对象被删除(通过onInput事件),数组的内容被重新显示

这在Meteor中应该很简单,下面是我正在使用的代码。但是,我遇到了以下问题:

当数组中的两个或多个对象最初具有相同的
inputValue
(如下面的代码中的两个“aaa”字符串)时,数组不会以反应方式正确重新显示。我希望留下一个包含“aaa”的输入字段,但该字段是空的

当删除的是最后一个相同输入值时(本例中为第二个“aaa”),或者当值不同时(例如,将“aaa”中的一个代码更改为“aba”使其按预期工作),都不会发生这种情况。请注意,输入字段之外的文本得到了正确更新,数据库本身也得到了正确更新

main.html

<body>
    {{#with getData}}
    <form>
        {{#each array1}}
            <input id="{{id}}" type="text" value="{{inputValue}}"> 
            (id:{{id}}, inputValue: {{inputValue}})<br>
        {{/each}}
    </form>
    {{/with}}
</body>
MyCollection = new Meteor.Collection('myCollection');

Meteor.methods({
    delete: function (colId, subItemId) {
        MyCollection.update({id: colId}, {$pull: {array1: {id: subItemId}}});
    }
});

if (Meteor.isServer) {
    // make sure collection is empty then insert 1 document
    if (MyCollection.find().count() !== 0) {
        MyCollection.remove({id: 101});
    }
    MyCollection.insert({
        id: 101,
        array1: [
            { id: 1, inputValue: "aaa" },
            { id: 2, inputValue: "aaa" }
        ]
    });
}

if (Meteor.isClient) {
    Template.body.helpers({
        getData: function () {
            if (MyCollection.findOne({})) { // wait until collection is available
                return MyCollection.findOne({id: 101});
            }
        }
    });
    Template.body.events({
        "input input": function (event) {
            if ($(event.target).val() == "") {
                Meteor.call("delete", 101, Number($(event.target).attr("id")));
            }
        }
    });
}
这是一个错误还是我遗漏了什么?我是Meteor的新手,所以我先把这个作为一个问题发布在这里。我正在使用Meteor 1.1.0.3,这是一个新的默认项目,并在最新版本的Safari和Firefox上进行了尝试