1

I have two files in my application, DesignFactory.js:

var fs = require('fs');
var dotenv = require('dotenv');
dotenv.load();
var designtokenfile = require ('./designtokenfile');
var designtokendb = require ('./designtokendb');
var TYPE=process.env.TYPE;
var DesignFactory={};
DesignFactory.storeDesign = function(TYPE) {
if (TYPE == 'file') {
  var data=design.designtokenfile.load();
  console.log(data);

} else if (TYPE == 'db') {
  return designtokendb;
}
};

module.exports.design=DesignFactory;

now, I have another designtokenfile.js file, designtokenfile.js:

var fs = require('fs');
var load = function() {
fs.readFile('output.txt', 'utf8', function (err,data) {

  return data;
 if (err) {
   return console.log(err);
 }
    });

 };

 module.exports.load=load;

So my problem is am not able get data returned from load method. when I print data inside storeDesign method returned from load function, it displays undefined. but I want contents of output.txt inside storeDesign method. Please help me.

Sushma
  • 21
  • 3
  • 9

3 Answers3

1

Instead of:

var load = function() {
  fs.readFile('output.txt', 'utf8', function (err, data) {
    return data;
     if (err) {
       return console.log(err);
     }
  });
};

which has no way of working because the if after the return would never be reached, use this:

var load = function(cb) {
  fs.readFile('output.txt', 'utf8', function (err,data) {
     if (err) {
       console.log(err);
       return cb(err);
     }
     cb(null, data);
  });
};

and use it like this:

load((err, data) => {
  if (err) {
    // handle error
  } else {
    // handle success
  }
});

Or use this:

var load = function(cb) {
  return new Promise(resolve, reject) {
     fs.readFile('output.txt', 'utf8', function (err, data) {
       if (err) {
         console.log(err);
         reject(err);
       }
       resolve(data);
     });
  });
};

and use it like this:

load().then(data => {
    // handle success
}).catch(err => {
    // handle error
});

In other words, you cannot return a value from a callback to asynchronous function. Your function either needs to tak a callback or return a promise.

You need to reed more about asynchronous nature of Node, about promises and callbacks. There are a lot of questions and answers on Stack Overflow that I can recommend:

Community
  • 1
  • 1
rsp
  • 107,747
  • 29
  • 201
  • 177
0

fs.readFile ist asynchrone so you need to pass a callback function or use fs.readFileSync

Philipp Beck
  • 449
  • 2
  • 14
0

You are getting undefined because of asynchronous nature.. Try for the following code:

var fs = require('fs');
var load = function(callback) {
fs.readFile('output.txt', 'utf8', function (err,data) {

  //return data;
   callback(null, data);
 if (err) {
   callback("error", null);
 }
    });

 };

 module.exports.load=load;

var fs = require('fs');
var dotenv = require('dotenv');
dotenv.load();
var designtokenfile = require ('./designtokenfile');
var designtokendb = require ('./designtokendb');
var TYPE=process.env.TYPE;
var DesignFactory={};
DesignFactory.storeDesign = function(TYPE) {
if (TYPE == 'file') {
  var data=design.designtokenfile.load(function(err, res){
   if(err){
   } else {
    console.log(data);
   }
}); 

} else if (TYPE == 'db') {
  return designtokendb;
}
};

module.exports.design=DesignFactory;
Subburaj
  • 5,114
  • 10
  • 44
  • 87