2014-04-19 41 views
0

我有一個樹形結構的節點,我需要能夠深入克隆。出於安全原因(以及性能),我希望在服務器上運行。我創建了可以在客戶端上正常工作的函數deepClone,但是當我將其更改爲Meteor.method時,崩潰時出現調用堆棧範圍超出的錯誤。Meteor.call使用遊標參數

的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 

我懷疑有一個簡單的解決這一點,但它躲避我。任何幫助表示讚賞。

回答

0

您的樹中的某個節點實際上可能指向其中一個祖先,從而導致無限循環嘗試走樹。

另一個問題是,您需要確保存儲在數據庫中或通過DDP發送的任何內容都可以強制轉換爲EJSON對象。否則,您也可能會看到此錯誤。

+0

我在客戶端運行代碼時檢查了代碼生成的樹,它運行良好。我懷疑我的語法錯誤是通過DDP發送databaseName(集合)。不幸的是,我在這方面的學習曲線很早。 – user3474498