Why is my make it modular solution not returning anything?

Hi I don’t understand why nothing is being returned with make it modular, not even the error message. Here is my code:

module file:

var fs = require('fs');
var path = require('path');
var files = [];
module.exports = function(_dirname, extension, callback){

fs.readdir(_dirname, function callback(err,list){
    if (err){
        return callback(err);
    }
    else{
        
    for (var i = 0; i < list.length; i++){
        if (path.extname(list[i]) === "." + extension)
        {
             files.push(list[i]);
        }
        
    }
    return callback(null.files);
    }
    
});
}


and my main file:

var mymodule=require('./mymodule.js');

mymodule(process.argv[2],process.argv[2], function(err,data){
    if (err){
        console.log(err);
        }
    else{
        for (var i = 0; i < data.length; i++){
            console.log(data[i]);
        }
        
        }
});

Thanks for any advice and help :slight_smile:

In your module, you’re returning your callback(err) function in the function definition rather than using console.log to display the error in the console.

yes this is what im meant to do isn’t it. return the err (if there is one) so that it can be printed when calling mymodule in the main file? …an early return? this isn’t the reason why nothing is not being printed.

I see where my mistake was. Sorry about that.

In your module you’re returning callback(null.files) rather than doing callback(null, files)

No this isnt the problem as i replaced this and its still not printing anything.
Thanks

Actually I also think this is the problem.

Your function mymodule is returning the callback, which in your case is

function(err,data){
    if (err){
        console.log(err);
        }
    else{
        for (var i = 0; i < data.length; i++){
            console.log(data[i]);
        }       
        }
}

This function has no results, hence the return statment has nothing to return.

You should only call the callback, not return it.

1 Like