调用node模块时,Undefined不是函数

Undefined is not a function when calling node module

本文关键字:函数 Undefined node 模块 调用      更新时间:2023-09-26

我在我的项目中分离了一些逻辑到不同的文件,问题是我得到了以下错误

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");

你知道我哪里做错了吗?这与异步调用

无关。

你的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)
});

关于模块。当导出在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: file.readFile(filePath, callback)

所以当你的模块文件。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;
});

我希望这能帮助你澄清一点关于回调的知识。

您在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);
    });
}