1

I have created a function to populate a list of objects but they are not being written.

if (stats.isDirectory()) {
        objlist=[];
        //do something here.. the path was correct
        fs.readdir(path, (err, files) => {
            objlist=[];
            for(file in files){
                //console.log(files[file])
                objlist[file]={};
                fs.stat(path + '\\' + files[file], function(err,stats){
                    if(err)
                        throw err;
                    if(stats.isDirectory()){
                        objlist[file]['file'] = 'folder'
                    }
                    if(stats.isFile()){
                        objlist[file]['file'] = 'file'
                    }
                    objlist[file]['name'] = files[file]
                    objlist[file]['size'] = stats.size
                    //console.log(objlist)
                    //console.log(stats)
                });
            }
        });
      console.log(objlist);
      return objlist;
    }

However the function returns an empty objlist; Can you please suggest what I am doing wrong

2
  • try using readdirsync. Also you should know that it's all async so return statement will not wait. See basics. Commented Mar 1, 2018 at 19:27
  • please notice that readdir() is async function Commented Mar 1, 2018 at 19:37

3 Answers 3

2

this code will be helphul. use let file in files

 objlist=[];
        //do something here.. the path was correct
        fs.readdir(path, (err, files) => {

            objlist=[]; 
            for(let file in files){
                objlist[file]={};
                fs.stat(path + '\\' + files[file], function(err,stats){
                    if(err)
                        throw err;
                    if(stats.isDirectory()){
                        objlist[file]['file'] = 'folder'
                    }
                    if(stats.isFile()){
                        objlist[file]['file'] = 'file'
                    }
                    objlist[file]['name'] = files[file]
                    objlist[file]['size'] = stats.size

                    if(file == files.length-1) {
                        console.log(objlist);
                    }
                });
            }
        });
Sign up to request clarification or add additional context in comments.

Comments

0

You must add the objects to the array with the method push, like this:

objlist=[];
for(file in files){
    obj = {};
    obj.file = file;
    //... do all stuff with obj
    objlist.push(obj);
}

Also your function is asynchronous inside the fs.readdir, so if you have to build and populate in the function or do an await to receive the result.

Otherwise you will call the function, it will be executed asynchronously, you will continue at the main function without the wait of the response of the fs.readdir and the array will continue empty because you async function may or may not have been executed.

Comments

0

There are two mistakes in your code:-

1) You are using an array as an object

2) Since for loop is synchronous loop and you are trying asynchronous task inside it and hence either use synchronous version of fs or change your for loop as follows:-

for(file in files) {
    obj = {};
    obj[file]={};
    (function(f){
                fs.stat(path + '\\' + files[f], function(err,stats){
                    if(err)
                        throw err;
                    if(stats.isDirectory()){
                        obj[file]['file'] = 'folder'
                    }
                    if(stats.isFile()){
                        obj[file]['file'] = 'file'
                    }
                    obj[file]['name'] = files[file]
                    obj[file]['size'] = stats.size
                    objlist.push(obj)
                    //console.log(objlist)
                    //console.log(stats)

                });
            }
    }(file))
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.