当我从前端调用 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
如果在控制台中运行更新调用,也会发生该错误。它发生在更新的第一次运行时,但到那时 react 性在附加到它的任何其他浏览器上都被破坏了。
编辑 这是触发更新的可点击项目的模板:
<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 应用程序的一个端口)
最佳答案
我发现这通常是重复 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}});
}
});
关于node.js - 更新集合时 Meteor.flush 中的 meteor 异常会中断客户端之间的 react 性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13458593/