Thursday, 20 April 2017

Undefined when returning value

I've node project. Root file is index.js and file helper.js, here I've some helper functions and it imported to index.js. I'm trying to get some data, using function in helper.js, but when I calling it in index.js it returning undefined. But in helper.js everething is OK, console.log showing data that I need. How I can fix this problem?

index.js file content:

const helper = require('./helper');

let data = helper.getData();
console.log(data); // undefined

helper.js file content:

const fs = require('fs');

module.exports = {
  getData: () => {
    fs.readFile('data.json', 'utf8', (err, data) => {
      const allData = JSON.parse(data);
      console.log(allData); // IS OK!
      return allData;
    });
  }
}



via Harard Dype

No comments:

Post a Comment