I'm sorry for, what might easily be a naive question, but I`m trying to figure out how node works, especially for a problem like this:
What I need do is to send an object/file from fs.readFile through require and module.exports. This is what I have tried is this
in one file (call it app.js) the code for reading a file:
var fs = require('fs');
var file_contents = undefined;
var callback_reader = function(err, data) {
if (err) return console.error(err);
file_contents = data.toString().split('\n');
}
module.exports = {
parseFile: function(file_path) {
fs.readFile(file_path.toString(), 'utf-8', callback_reader);
}
}
and in some other file, (call it main.js) I need to use the contents of the file read by the readFile like this
var file_importer = require('./app.js')
file_importer.parseFile(real_path_to_file);
but if i try console.log of this last line I always get undefined object. Now I know it is because callback does not execute before the console.log but I`m unsure how to achieve this munication.
I'm sorry for, what might easily be a naive question, but I`m trying to figure out how node works, especially for a problem like this:
What I need do is to send an object/file from fs.readFile through require and module.exports. This is what I have tried is this
in one file (call it app.js) the code for reading a file:
var fs = require('fs');
var file_contents = undefined;
var callback_reader = function(err, data) {
if (err) return console.error(err);
file_contents = data.toString().split('\n');
}
module.exports = {
parseFile: function(file_path) {
fs.readFile(file_path.toString(), 'utf-8', callback_reader);
}
}
and in some other file, (call it main.js) I need to use the contents of the file read by the readFile like this
var file_importer = require('./app.js')
file_importer.parseFile(real_path_to_file);
but if i try console.log of this last line I always get undefined object. Now I know it is because callback does not execute before the console.log but I`m unsure how to achieve this munication.
Share Improve this question asked Jun 4, 2015 at 16:20 Fedja BlagojevicFedja Blagojevic 8471 gold badge10 silver badges18 bronze badges 2- You are not returning the file_contents from callback_reader and you need var file = file_importer.parse.... console.log(file) – Molda Commented Jun 4, 2015 at 17:09
- Ok if I use return file_contents in app.js's callback_reader and call console log in main.js like @molda described I still get undefined in the log. – Fedja Blagojevic Commented Jun 4, 2015 at 18:02
2 Answers
Reset to default 5So i changed your code a little bit to use callbacks. It seems that you can't use "return" from asyncronous function in module.exports. However, the code bellow works as expected. Hope it helps.
main.js
var file_importer = require('./app.js')
file_importer.parseFile('./time.js', function(err, data){
if(err) return console.log(err);
console.log(data);
});
app.js
var fs = require('fs');
module.exports = {
parseFile: function(file_path, callback) {
fs.readFile(file_path.toString(), 'utf-8', function(err, data) {
if (err) return callback(err);
callback(null, data);
});
}
}
// much shorter version
exports.parseFile = function(file_path, callback) {
fs.readFile(file_path.toString(), 'utf-8', callback);
}
This is javascript work, it don't wait the callback was called to return.
You should do your console.log
in your callback.
Like these :
fs.readFile(pathToFile, 'utf-8', function(err, data) {
if (err) return err;
console.log(data);
// Continue your process here
})