3

Im using the fs module of node.js to read all the files of a directory and return their content, but the array i use to store the content is always empty.

server-side:

app.get('/getCars', function(req, res){
   var path = __dirname + '/Cars/';
   var cars = [];

   fs.readdir(path, function (err, data) {
       if (err) throw err;

        data.forEach(function(fileName){
            fs.readFile(path + fileName, 'utf8', function (err, data) {
                if (err) throw err;

                files.push(data);
            });
        });
    });
    res.send(files);  
    console.log('complete'); 
});

ajax function:

$.ajax({
   type: 'GET',
   url: '/getCars',
   dataType: 'JSON',
   contentType: 'application/json'
}).done(function( response ) {
      console.log(response);
});

Thanks in advance.

5
  • you are doing it wrong, you are sending results without knowing that fs.readFile is running async on each file, that means file is not read niether contents are pushed to array and array is already sent to client Commented Mar 6, 2016 at 6:21
  • 2
    I don't know who down-voted this question, If this questions is invalid one should comment while down-voting. All questions are not dumb, If some one is new to async style of paradigm he/she will do such coding. Commented Mar 6, 2016 at 7:27
  • I though that my problem was that i was using async methods but i tried using their sync counterparts (readdirSync,readFileSync) and still didn't get the expected result. Commented Mar 6, 2016 at 7:31
  • 1
    don't do sync coding, make most of async, see the simulated version here it works very well, github.com/zishon89us/node-cheat/blob/master/files/… Commented Mar 6, 2016 at 7:36
  • It really works well, how can i learn more about the async functions on node.js?. Btw thanks for all your help! Commented Mar 6, 2016 at 7:46

1 Answer 1

8

Read content of all files inside a directory and send results to client, as:

choice 1 using npm install async

var fs = require('fs'),
    async = require('async');

var dirPath = 'path_to_directory/'; //provice here your path to dir

fs.readdir(dirPath, function (err, filesPath) {
    if (err) throw err;
    filesPath = filesPath.map(function(filePath){ //generating paths to file
        return dirPath + filePath;
    });
    async.map(filesPath, function(filePath, cb){ //reading files or dir
        fs.readFile(filePath, 'utf8', cb);
    }, function(err, results) {
        console.log(results); //this is state when all files are completely read
        res.send(results); //sending all data to client
    });
});

choice 2 using npm install read-multiple-files

var fs = require('fs'),
    readMultipleFiles = require('read-multiple-files');

fs.readdir(dirPath, function (err, filesPath) {
    if (err) throw err;
    filesPath = filesPath.map(function (filePath) {
        return dirPath + filePath;
    });
    readMultipleFiles(filesPath, 'utf8', function (err, results) {
        if (err)
            throw err;
        console.log(results); //all files read content here
    });
});

For complete working solution get this Github Repo and run read_dir_files.js

Happy Helping!

Sign up to request clarification or add additional context in comments.

4 Comments

After npm install async ;)
It worked like a charm, thanks!. I just have one doubt, what is the function of the cb parameter on the async.map() function?
cb is callback that is supposed to be called when some-work is done, like file read is complete in this case, for that you will have to understand asynchronous nature :)
I'll deepen more into the asynchronous nature. Thanks for all your help!

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.