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

时间:2021-09-28 23:16:15

I've separate some logic to different file in my project, the problem is that I got the following error

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

Cannot read property 'readFile' of undefined

this is the structure of my project

这是我项目的结构

projName
   utils
     file.js

the file.js code is

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

I want to call to this module at

我想打电话给这个模块

projname
    controller
         request

I do it with the following code And here I got the error

我用下面的代码做到这里我得到了错误

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

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

any idea what am I doing wrong here? This is not related to async call

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

2 个解决方案

#1


2  

Your file.js could be like this:

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

var fs = require('fs');

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

and your request.js file like this:

和你的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

}

Regarding the async call when you call a method from Node.JS libaries, it is usually a async call, that means that the result of the function is not going to return immediately:

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

var data = fs.readFile(filePath);

instead it is going to return at some time later, so the only way you can get the results later is passing by a function that is going to be called when the results are ready:

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

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

Regarding module.exports when you export some logic of a file that you created in Node.JS, you can return your function on the following ways:

关于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';

UPDATE: In your file.js you are exporting a function that is going to receive a file path and a function called callback as the second parameter (yes, you read it well, a function as a parameter) which is going to be called once the fs.readFile get the file content.

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

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

then in your request.js file, you are using your module (file.js) you just created, and the function that your module is exporting accepts a string as parameter called filePath, and a function as parameter called callback: file.readFile(filePath, callback)

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

so when your module file.js get the content file is going to call to your callback function parameter.

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

I hope that this helps to clarify a little bit your knowledge about callbacks.

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

#2


1  

You don't return anything in your module.exports function. In fact, you don't even need to set module.exports to a function. Just simply export your function:

您不会在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;

Also, your return data isn't going to work, as fs.readFile is asynchronous. You need to use callbacks. So, your readFile function might look more like:

此外,您的返回数据不起作用,因为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);
    });
}

#1


2  

Your file.js could be like this:

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

var fs = require('fs');

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

and your request.js file like this:

和你的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

}

Regarding the async call when you call a method from Node.JS libaries, it is usually a async call, that means that the result of the function is not going to return immediately:

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

var data = fs.readFile(filePath);

instead it is going to return at some time later, so the only way you can get the results later is passing by a function that is going to be called when the results are ready:

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

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

Regarding module.exports when you export some logic of a file that you created in Node.JS, you can return your function on the following ways:

关于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';

UPDATE: In your file.js you are exporting a function that is going to receive a file path and a function called callback as the second parameter (yes, you read it well, a function as a parameter) which is going to be called once the fs.readFile get the file content.

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

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

then in your request.js file, you are using your module (file.js) you just created, and the function that your module is exporting accepts a string as parameter called filePath, and a function as parameter called callback: file.readFile(filePath, callback)

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

so when your module file.js get the content file is going to call to your callback function parameter.

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

I hope that this helps to clarify a little bit your knowledge about callbacks.

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

#2


1  

You don't return anything in your module.exports function. In fact, you don't even need to set module.exports to a function. Just simply export your function:

您不会在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;

Also, your return data isn't going to work, as fs.readFile is asynchronous. You need to use callbacks. So, your readFile function might look more like:

此外,您的返回数据不起作用,因为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);
    });
}