我有一个节点的树形结构,我需要它来进行深度克隆。出于安全原因(以及性能),我希望它在服务器上运行。我创建了函数deepClone,该函数在客户机上运行良好,但当我将其更改为Meteor.method时,由于出现调用堆栈范围超出错误而崩溃。
Meteor.method代码:
Meteor.methods({
deepClone: function(oldParentNodeID, databaseName) {
if (oldParentNodeID && databaseName)
{
console.log(oldParentNodeID);
console.log(databaseName);
// oldParentNodeID is expected to be the _id of a headerNode with a field called archives[{Major:, Minor:, archivedParentNode}]
// databaseName is the name of the collection where the cloning occurs
if (!(oldParentNodeID))
{
return; //need to add error code
}
else
{
var clonedTree=recursiveClone(oldParentNodeID,databaseName);
databaseName.update({_id:oldParentNodeID},{$push: {archivedStack: clonedTree}});
}}
}});
var recursiveClone = function(currentNodeID, databaseName) {
//this function clones the node whos ID is passed and all descendents of that node.
var oldParentNode=(databaseName).findOne({_id: currentNodeID});
var archivedParentNode=new Object;
var i, childNode;
jQuery.extend(archivedParentNode,oldParentNode);
delete archivedParentNode._id;
archivedParentNode.subcategories=[];
var retval= databaseName.insert(archivedParentNode);
if ((oldParentNode.subcategories) && (oldParentNode.subcategories.length)){
for (i=0; i< oldParentNode.subcategories.length; i++) {
childNode=recursiveClone(oldParentNode.subcategories[i],databaseName);
archivedParentNode.subcategories.push(childNode);
databaseName.update({_id:retval},{$push: {subcategories: childNode}});
databaseName.update({_id:childNode},{$set: {parentCategory: retval}});
}
}
return retval;
};请注意,子类别是子_ids的数组,而parentCategory是父类别的_id。
运行此命令时出现的错误为:
RangeError {stack: "RangeError: Maximum call stack size exceeded↵ a…s?8659e45802513ffad53befd6dbee7e6e80df9da4:425:22", message: "Maximum call stack size exceeded"}
message: "Maximum call stack size exceeded"
stack: "RangeError: Maximum call stack size exceeded↵ at http://localhost:3000 /packages/ejson.js?8659e45802513ffad53befd6dbee7e6e80df9da4:424:22↵ at Function._.each._.forEach (http://localhost:3000/packages/underscore.js?0a80a8623e1b40b5df5a05582f288ddd586eaa18:164:22)↵ at Object.EJSON.clone (http://localhost:3000/packages/ejson.js?8659e45802513ffad53befd6dbee7e6e80df9da4:424:5)↵ at http://localhost:3000/packages/ejson.js?8659e45802513ffad53befd6dbee7e6e80df9da4:425:22↵ at Function._.each._.forEach (http://localhost:3000/packages/underscore.js?0a80a8623e1b40b5df5a05582f288ddd586eaa18:164:22)↵ at Object.EJSON.clone (http://localhost:3000/packages/ejson.js?8659e45802513ffad53befd6dbee7e6e80df9da4:424:5)↵ at http://localhost:3000/packages/ejson.js?8659e45802513ffad53befd6dbee7e6e80df9da4:425:22↵ at Function._.each._.forEach (http://localhost:3000/packages/underscore.js?0a80a8623e1b40b5df5a05582f288ddd586eaa18:164:22)↵ at Object.EJSON.clone (http://localhost:3000/packages/ejson.js?8659e45802513ffad53befd6dbee7e6e80df9da4:424:5)↵ at http://localhost:3000/packages/ejson.js?8659e45802513ffad53befd6dbee7e6e80df9da4:425:22"
__proto__: Error
RangeError: Maximum call stack size exceeded
at http://localhost:3000/packages /ejson.js?8659e45802513ffad53befd6dbee7e6e80df9da4:424:22
at Function._.each._.forEach (http://localhost:3000/packages/underscore.js?0a80a8623e1b40b5df5a05582f288ddd586eaa18:164:22)
at Object.EJSON.clone (http://localhost:3000/packages/ejson.js?8659e45802513ffad53befd6dbee7e6e80df9da4:424:5)
at http://localhost:3000/packages/ejson.js?8659e45802513ffad53befd6dbee7e6e80df9da4:425:22
at Function._.each._.forEach (http://localhost:3000/packages/underscore.js?0a80a8623e1b40b5df5a05582f288ddd586eaa18:164:22)
at Object.EJSON.clone (http://localhost:3000/packages/ejson.js?8659e45802513ffad53befd6dbee7e6e80df9da4:424:5)
at http://localhost:3000/packages/ejson.js?8659e45802513ffad53befd6dbee7e6e80df9da4:425:22
at Function._.each._.forEach (http://localhost:3000/packages/underscore.js?0a80a8623e1b40b5df5a05582f288ddd586eaa18:164:22)
at Object.EJSON.clone (http://localhost:3000/packages/ejson.js?8659e45802513ffad53befd6dbee7e6e80df9da4:424:5)
at http://localhost:3000/packages/ejson.js?8659e45802513ffad53befd6dbee7e6e80df9da4:425:22 我怀疑有一个简单的解决方法,但它是我躲避。任何帮助都是非常感谢的。
发布于 2014-04-20 05:09:02
有可能树中的节点实际上指向它的祖先之一,导致在尝试遍历树时出现无限循环。
另一个问题是,您需要确保存储在数据库中或通过DDP发送的任何内容都可以被强制到EJSON对象中。否则,您也可能会看到此错误。
https://stackoverflow.com/questions/23173690
复制相似问题