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

Undefined is not a function when calling node 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");

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

您的 module.exports 功能中没有 return 任何内容。事实上,您甚至不需要将 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);
    });
}

你的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 库调用方法时,通常是异步调用,这意味着该函数的结果不会立即 return:

var data = fs.readFile(filePath);

相反,它会在稍后的某个时间 return,因此您稍后可以获得结果的唯一方法是传递一个将在结果准备就绪时调用的函数:

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

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

// 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 中,您正在导出一个将接收文件路径的函数和一个称为回调的函数作为第二个参数(是的,您读得很好,一个函数作为参数),它将被调用一次fs.readFile 获取文件内容。

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

然后在您的 request.js 文件中,您正在使用您刚刚创建的模块 (file.js),并且您的模块正在导出的函数接受一个字符串作为名为 filePath 的参数,以及一个函数作为参数调用回调:file.readFile(filePath, callback)

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

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

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

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

我希望这有助于澄清您对回调的一些了解。