Recursive function on Alfresco JS Api ECM

cancel
Showing results for 
Search instead for 
Did you mean: 
mboquillon
Member II

Recursive function on Alfresco JS Api ECM

Hello,

I try to do a recursive function to get all files in a node (even if the files are in different subfolders) and to stock them into an array. But the array I return is unexploitable..

The code I use : 

function fillArray(alfrescoJsApi: AlfrescoJSApi, node:any, array: Array<any>) {
   alfrescoJsApi.nodes.getNodeChildren(node).then( function (data) {
   for(var entry of data.list.entries) {
      if(entry.entry.isFolder == true) {
        let id = entry.entry.id;
        let newAlfrescoApi = <AlfrescoJSApi> new AlfrescoJSApi({
          provider: 'ECM'
        });
        fillArray(newAlfrescoApi, id, array);
      } else if(entry.entry.isFile == true) {
        array.push( {[entry.entry.id]: entry.entry.name});
      }
    };
    
  }, function (error) {
    console.log('This node does not exist');
  });
  return array;
}

Any ideas ?

 

Thanks

2 Replies
Moderator
Moderator

Re: Recursive function on Alfresco JS Api ECM

First of all, I would recommend moving the JS-API outside the foreach, as you create a lot of client objects and that can hurt performance.

Second, you are using async method call, and then apply for loop where you fire more async things. I think the entire function is not waiting for results correctly.

mboquillon
Member II

Re: Recursive function on Alfresco JS Api ECM

Thanks for the answer.

 

I move the JS-API as you suggested. 

I have an "array" in return, with Objects in it. I can see it in console.log

But I can't use it. For example, array[0] returns undefined.