调用节点模块时,未定义不是函数

时间:2015-06-09 14:20:54

标签: javascript node.js module

我已将某些逻辑分离到我项目中的不同文件中, 问题是我收到了以下错误

Cannot read property 'readFile' of undefined

这是我项目的结构

projName
   utils
     file.js

file.js代码是

module.exports = function () {
   var  fs = require('fs');
    function readFile(filePath) {
        fs.readFile(filePath, 'utf8', function (err, data) {
            if (err) {
                return console.log("error to read file:  " + filePath + " " + err);
            }
            return data;
        });
    }
};

我想在

打电话给这个模块
projname
    controller
         request

我使用以下代码执行此操作 在这里我收到了错误

   module.exports = function (app) {
        app.get('/test', function(req, res) {

            var file = require('../utils/file')();
            var fileContent = file.readFile("C://test.txt");

知道我在这里做错了什么? 这与异步调用无关

2 个答案:

答案 0 :(得分:2)

你的file.js可能是这样的:

var fs = require('fs');

module.exports.readFile = function (filePath, cb) {
  fs.readFile(filePath, 'utf8', cb);
};

和你的request.js文件是这样的:

var file = require('../utils/file');

module.exports = function (app) {

  var fileContent = '';
  var filePath = 'C://test.txt';

  file.readFile(filePath, function (err, data) {
    if (err) {
        return console.log("error to read file:  " + filePath + " " + err);
    }

    console.log(data);
    fileContent = data;
  }); 

  // some content

}

关于异步通话 当你从Node.JS库中调用一个方法时,它通常是一个异步调用,这意味着该函数的结果不会立即返回:

var data = fs.readFile(filePath);

相反它会在稍后的某个时间返回,所以你以后获得结果的唯一方法是传递一个在结果准备就绪时调用的函数:

fd.readFile(filePath, function dataReady (err, data) {
  console.log(data)
});

关于module.exports ,当您导出在Node.JS中创建的文件的某些逻辑时,您可以通过以下方式返回您的函数:

// exporting a function, myModule.js
module.exports = function () { 
  console.log('hello');
};

// consuming myModule.js
var someFunction = require('./myModule.js');
someFunction(); // prints 'hello';
// exporting a function, myModule.js
module.exports.readFile = function () {
  console.log('hello');
};

// consuming myModule.js
var myModule = require('./myModule.js');
myModule.readFile(); // prints 'hello';

<强>更新: 在你的file.js中,你要导出一个将要接收文件路径的函数和一个名为callback的函数作为第二个参数(是的,你读得很好,函数作为参数),这将被调用一次fs .readFile获取文件内容。

module.exports.readFile = function (filePath, callback) {
  fs.readFile(filePath, 'ut8', function (err, fileContent) {
    callback(err, fileContent);
  });
}

然后在你的request.js文件中,你正在使用你刚刚创建的模块(file.js),你的模块导出的函数接受一个字符串作为名为filePath的参数,一个函数作为参数叫做callback:{ {1}}

所以当你的模块file.js获取内容文件时,将调用你的回调函数参数。

file.readFile(filePath, callback)

我希望这有助于澄清一些关于回调的知识。

答案 1 :(得分:1)

您不会在module.exports函数中返回任何内容。实际上,您甚至不需要将module.exports设置为函数。只需输出您的功能:

var  fs = require('fs');
function readFile(filePath) {
    fs.readFile(filePath, 'utf8', function (err, data) {
        if (err) {
            return console.log("error to read file:  " + filePath + " " + err);
        }
        return data;
    });
}
exports.readFile = readFile;

此外,您的return data无效,因为fs.readFile是异步的。你需要使用回调。因此,您的readFile函数可能看起来更像:

function readFile(filePath, callback) {
    fs.readFile(filePath, 'utf8', function (err, data) {
        if (err) {
            return console.log("error to read file:  " + filePath + " " + err);
        }
        callback(data);
    });
}