简体   繁体   English

Javascript(节点)错误:意外的令牌功能

[英]Javascript (node) error: Unexpected token function

I am trying the learn younode workshop, the make it modular step to be precise. 我正在尝试学习younode研讨会,使其成为精确的模块化步骤。 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 我的代码是这个,您也可以在以下链接中找到它: 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;
}

您的问题是您要在函数声明中声明函数。

It should be like this 应该是这样

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

And then In you app.js you can use 然后在app.js中,您可以使用

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. 删除函数内部的匿名函数,希望它对您有用。

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;

        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM