问题描述
我正在使用回调读取目录和导出文件名,但是当我导入readDirectory函数时我的模块没有发送数据,不知道下面的代码出了什么问题?我的回调函数返回模块本身的值,但是在使用导出时它不会发送。
我尝试过:
main.js
var fs = require('fs');
var path =' ./Logs';
函数readDirectory(){
fs.readdir(路径,函数(错误,项目){
返回filesData(项目);
});
}
函数filesData(项目){
var data = JSON。 stringify(items)
console.log('来自模块的数据',数据);
返回数据;
}
exports.readDirectory = readDirectory;
app.js
var readDirectory = require('./ main');
var obj = readDirectory.readDirectory();
console.log(JSON.stringify(obj));
I am reading directory and exporting file names using callbacks but my module is not sending data when i import readDirectory function, Any idea what is going wrong with below code ? my callback return values in module itself but its not sending while using exports.
What I have tried:
main.js
var fs = require('fs');
var path = './Logs';
function readDirectory() {
fs.readdir(path, function(err,items) {
return filesData(items);
});
}
function filesData(items) {
var data = JSON.stringify(items)
console.log('data from module',data);
return data;
}
exports.readDirectory = readDirectory;
app.js
var readDirectory = require('./main');
var obj = readDirectory.readDirectory();
console.log(JSON.stringify(obj));
这篇关于如何导出节点模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!