Copy a tree recursively in Meteorjs

Course Queries Syllabus Queries 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Syllabus Queries related to Course Queries. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

I am working on a Meteorjs application where I have a syllabus and I want to copy or clone this syllabus. Here in my syllabus there are modules which can till multiple levels means a mudule can have another module as its child module and so on.for example i have a module Grammer and grammer has another module named tenses and tenses ahs some other module named present past and future these will seems like this.

tenses
    past
        simple
        negative
        interogative
    present
        simple
        negative
        interogative
    future
        simple
        negative
        interogative

databse fields are name, parent_id, has_children, syllabus_id these mudules can further have some sub modules or children. In database to check if a module have further children i have a field boolean has_children and parent_id to relate the child with the parent.If element is a root element its 'parent_id` will be null.

for this i tried to reach every element recursively but i am able to reach only one element at one level. means i am not reaching to the siblings of an element but the children.

My code is :

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)    
    }
}

and the other function which is creating the objects recursively is

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(i=0;i<child_items.length;i++){
            makeClone(child_items[i],item_id,syllabi_id);
        }
    }
}

But using this i am able to retrieve only one element at the same level but not its siblings. i will be able to create tenses past simple negative interogative tell me guys where i am making a mistake while retrieving these objects.Or tell me any otherway to accomplish the task. Here i have to change only the syllabus_id and create all the objects again.

profilepic.png
manpreet 2 years ago

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


0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.