0

I am trying the learn younode workshop, the make it modular step to be precise. I wrote the code in the link and am getting the "error in the title". I have checked brackets and parenthesis but can not seem to find where i got it wrong. Any help would be appreciated (i just started with node). My code is this and you can find it also at this link: http://pastebin.com/G8x2GH7h

module.exports = function (directoryPath, extention, function(error, arrayOfNames) {
        var fs = require('fs');
        var path = require('path');
        var FileNamesArray = [];
        fs.readdir(directoryPath,function (error,list){
                list.forEach(function (file) {
                        if (path.extname(file) === '.' + extention) {
                                FileNamesArray.push(file);
                        }
                });
        });
        return FileNamesArray;
        }){
        return ArrayOfNames;
}
5
  • What is ArrayOfNames up to? Commented May 29, 2015 at 10:05
  • The function you have at (directoryPath, extention, function(error, arrayOfNames) is a syntax error. Commented May 29, 2015 at 10:07
  • function(error, arrayOfNames) {...} is not valid where it is. It's not clear to me what you are trying to do with that. Commented May 29, 2015 at 10:14
  • @Armand: for the task we demand that you crate a module that would filter file names in a given directory to call it from another file and print it to the console from there. Commented May 29, 2015 at 10:54
  • @FelixKling: That function is creating an array containing the names of the files that have the given extesion. Commented May 29, 2015 at 10:54

3 Answers 3

1

Your problem is that you are declaring function inside of function declaration.

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

6 Comments

It's perfectly valid to define functions inside other functions.
Yes, it is valid to define functions inside of other function body, but not valid to define them at position of argument in function declaration. It will give syntax error.
No, you can define an inline function as a parameter. That's the usual way when you define a callback function
You can define inline function as parameter when you are calling another function, not when you are declaring another function.
@pisamce: so according to you i should first declare my function and then pass it as an argument to the other one ?
|
0

It should be like this

exports.functionName = function() {
  // your code
};

And then In you app.js you can use

var abc = require('path to file name');
abc.functionName();

example

// square.js
var PI = Math.PI;

exports.area = function (r) {
  return PI * r * r;
};

exports.circumference = function (r) {
  return 2 * PI * r;
};

// app.js
 var square = require("./square.js");

var mySquare = square(2);
console.log('The area of my square is ' + mySquare.area());

Remove the anonymous function inside your function and hopefully it should work for you.

1 Comment

Thanks for the answer but according to liangzan.net/blog/blog/2012/06/04/how-to-use-exports-in-nodejs there is two different ways for the export. In the task i am give they specified that you should use the module.exports and not the exportes.function method.
0

You are missing the title of you function in the export section, should be in this way:

module.exports = {
getFileNamesList : function (directoryPath, extention, function(error, arrayOfNames) {
            var fs = require('fs');
            var path = require('path');
            var FileNamesArray = [];
            fs.readdir(directoryPath,function (error,list){
                    list.forEach(function (file) {
                            if (path.extname(file) === '.' + extention) {
                                    FileNamesArray.push(file);
                            }
                    });
            });
            return FileNamesArray;
            }){
            return ArrayOfNames;

        }
    }

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.