title: Creating tree recursively using JavaScript
body: <p>I want to create a tree using a recursive function. The input to this function is a node and I want to add its children to it with that recursive function.</p>
  
  <p>The following code will explain my problem in a better way:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function getUpstreamChildrenRecusrively(node) {
    var receiverId = localStorage.getItem("ReceiverId");
      //API call to get the children node
      axios({
        method: 'get',
        url: window.location.origin+"/api/rwa/coverageView/getUpstreamChildren?id="+node.elementId,
        headers: {
          "ReceiverId":receiverId
        }
      })
        .then(response =&gt; {        
          localStorage.setItem("ReceiverId",response.headers["receiverid"]);
          var data = response.data;                                      
          for(var i = 0; i &lt; data.length; i++) {
            var obj = data[i];                  
            var result = {};                                                
            result.text = obj.print;  
            result.elementId =  obj.id;
            result.elementText = obj.text; 
            result.expanded = true;
            result.visible = true; 
            result.icon = window.location.origin+"/api"+obj.image;        
            getUpstreamChildrenRecusrively(result);                       
            
            node.nodes = []; //nodes property will contain children
            node.nodes.push(result); 
            console.log("Tree so far:"+JSON.stringify(node));
          }                                     
        })    
      .catch(error =&gt; {      
      })         
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>For every recursive call, the value of the node is a separate node having a single child in nodes property. I want to see the node to be grown with all its children as a final result.</p>
  
  <p>What am I missing in this code?</p>
  
  <p>Thank you in advance!</p>