调用node模块时,Undefined不是函数
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");
你知道我哪里做错了吗?这与异步调用
你的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);
});
}
- 为什么会出现错误;未捕获的类型错误:undefined不是函数;
- 为什么会出现错误,“UncaughtTypeError:undefined不是函数”
- mongoose.connect undefined不是一个函数
- Uncaught TypeError:undefined不是函数-ember js
- jQuery Mobile Undefined不是一个函数
- 未捕获的类型错误:undefined不是函数$
- 使用在函数外部声明的变量的 Javascript 返回值 + undefined
- 为什么Grunt Copy给了我一个'undefined不是函数'警告
- Bootstrap-“;未捕获的类型错误:undefined不是函数;
- Undefined不是函数ImpressPages DatePicker
- 尝试使用.ninnerHTMLlog打印数据,获取错误Uncaught TypeError:undefined不是函数
- 得到“;undefined不是函数“;错误
- Undefined不是Fullcalendar js上的函数
- 使用谷歌地图:一个返回undefined的自定义javascript函数
- jqGrid:undefined不是函数
- Jquery,Uncaught TypeError:undefined不是函数
- $http.post undefined 不是函数
- Ember cli Uncatch TypeError: undefined 不是函数
- Infragistics igGrid Uncatch TypeError: undefined 不是一个函数
- 如果构造函数在另一个函数中,则新创建的对象调用构造函数undefined