4

当我从前端调用 Collection.update 时(允许方法调用),更新确实可以正常工作,但是抛出了下面的异常(在 Chrome 的 JS 控制台中,而不是在服务器中)。尽管发生了更新,但连接到同一集合的其他客户端在刷新浏览器之前看不到更新——我怀疑是因为异常。

知道是什么原因造成的吗?

Exception from Meteor.flush: Error: Can't create second landmark in same branch
    at Object.Spark.createLandmark (http://checkadoo.com/packages/spark/spark.js?8b4e0abcbf865e6ad778592160ec3b3401d7abd2:1085:13)
    at http://checkadoo.com/packages/templating/deftemplate.js?7f4bb363e9e340dbaaea8d74ac670af40ac82d0a:115:26
    at Object.Spark.labelBranch (http://checkadoo.com/packages/spark/spark.js?8b4e0abcbf865e6ad778592160ec3b3401d7abd2:1030:14)
    at Object.partial [as list_item] (http://checkadoo.com/packages/templating/deftemplate.js?7f4bb363e9e340dbaaea8d74ac670af40ac82d0a:114:24)
    at http://checkadoo.com/packages/handlebars/evaluate.js?ab265dbab665c32cfd7ec343166437f2e03f1a54:349:48
    at Object.Spark.labelBranch (http://checkadoo.com/packages/spark/spark.js?8b4e0abcbf865e6ad778592160ec3b3401d7abd2:1030:14)
    at branch (http://checkadoo.com/packages/handlebars/evaluate.js?ab265dbab665c32cfd7ec343166437f2e03f1a54:308:20)
    at http://checkadoo.com/packages/handlebars/evaluate.js?ab265dbab665c32cfd7ec343166437f2e03f1a54:348:20
    at Array.forEach (native)
    at Function._.each._.forEach (http://checkadoo.com/packages/underscore/underscore.js?772b2587aa2fa345fb760eff9ebe5acd97937243:76:11) 

编辑 2

如果在控制台中运行更新调用,也会发生错误。它发生在更新的第一次运行时,但到那时,连接到它的任何其他浏览器上的反应性都会被破坏。

编辑 这是触发更新的可点击项目的模板:

<template name="list_item">
  <li class="checklistitemli">
  <div class="{{checkbox_class}}" id="clitem_{{index}}">
    <input type="checkbox" name="item_checked" value="1" id="clcheck_{{index}}" class="checklist_item_check" {{checkbox_ticked}}> {{title}}
  </div>
  </li>
</template>

这是点击“list_item”的事件处理程序:

Template.list_item.events = {
  'click .checklistitem' : function(ev) {
    this.checked = !this.checked; 
    var updateItem = {}; 
    updateItem['items.'+this.index+'.checked'] = this.checked; 
    console.log("The error happens here");
    Lists.update({_id: this._id}, {$set:updateItem}, {multi:false} , function(err) { 
      console.log("In callback, after the error"); 
    });
  }
}

整个内容可在http://checkadoo.com 获得(它是我的基于 Tornado 的 Python 应用程序的一个端口)

4

1 回答 1

0

我发现这通常是重复 ID 的问题。@Harel 的问题不包括实际呈现集合的代码,因此我们无法说出他的问题的根本原因,但 @Drew 在评论中提供的重现抛出此错误,因为它正在呈现具有重复的“_id”值。

我已经整理了@Drew 复制的工作版本: https ://github.com/alanning/meteor-buggy-fix

关键修复是确保插入的饮料项目没有重复的“_id”字段:

  Template.drink_from_menu.events({
    'click .buy_drink': function () {
      var drink = {name: this.name, price: this.price};

      console.log("this = ", this);

      // using 'this' doesn't work because it already has a '_id' field and 
      // you would be trying to render multiple drinks with the same id
      // which causes Spark to throw the "Can't create second landmark 
      // in same branch" error
      //Tabs.update({_id:Session.get('tabId')}, {$push: {ordered_drinks: this}});

      // either ensure that the objects you want Spark to render have a 
      // unique id or leave off the _id completely and let Spark do it for you
      Tabs.update({_id:Session.get('tabId')}, {$push: {ordered_drinks: drink}});
    }
  });
于 2013-11-22T17:34:34.673 回答