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.

2

3 Answers 3

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:

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.