Everything is perfect in your code just add recursive function termination statement in your code. cloneSyllabus:function(syllabi_id){ syll_obj=Meteor.syllabi.findOne({_id:syllabi_id}); new_syllabi_id=Meteor.syllabi.insert({
subject_id:syll_obj.subject_id,
level_id:syll_obj.level_id,
});
var parent_objs = Meteor.collecton.find({
parent_id:null,
syllabus_id:syllabi_id}).fetch()
for(var i=0;i<syll_design_objs.length;i++){
makeClone(parent[i],null,new_syllabi_id)
}
return
}
And declare the variable i
as var i
and child_objs
as var child_objs
in loop of the function makeClone
because these variable are being treated here as a glabal variable in your code.
function makeClone(item,parent_id,syllabi_id){
var item_id=item._id
delete item._id
item.parent_id=parent_id
item.syllabus_id=syllabi_id
var new_parent_id=Meteor.syllabi_design.insert(item)
if(item.has_children){
child_items=Meteor.collection.find({parent_id:item_id}).fetch();
for(var j=0;j<child_items.length;j++){
makeClone(child_items[i],item_id,syllabi_id);
}
}
else{
return
}
}
Hope this will work perfect for you