0

I just tried to make one function that will take CSV files and return JSON data. when I console out the data it will work fine but when I tried to return the same data it will return undefined.

var fs = require('fs');

//sorting the json...
function compareStrings(a, b) {
  a = a.toLowerCase();
  b = b.toLowerCase();

  return (a < b) ? -1 : (a > b) ? 1 : 0;
}

//reding csv and converting it into json..........
var csv_to_json = ()=>{
    fs.readFile('./Data/book.csv',(err,data)=>{
        let jsonData = [];
        var content = data.toString().split('\n').join().split('\r');
        var key = content[0].split(';');
        content.shift();

        content.forEach((item)=>{
            var temp = item.replace(",").split(';');
            var data = {}
            for(var i=0;i<key.length;i++){
                data[key[i]] = temp[i]
            }

            jsonData.push(data);
        })

        jsonData.sort(function(a, b) {
            return compareStrings(a.title, b.title);
        })

    // console.log is working fine but when I tried to return the same data it won't work properly

        // console.log(jsonData);
        return jsonData;
    })
}

console.log(csv_to_json())

1 Answers1

0

Because you use a callback function and when use callback function you code execute asynchronously

I suggest you read this document

You have to change code like that:

var fs = require('fs');

//sorting the json...
function compareStrings(a, b) {
  a = a.toLowerCase();
  b = b.toLowerCase();

  return (a < b) ? -1 : (a > b) ? 1 : 0;
}

//reding csv and converting it into json..........
var csv_to_json = (callback)=>{
    fs.readFile('./Data/book.csv',(err,data)=>{
        let jsonData = [];
        var content = data.toString().split('\n').join().split('\r');
        var key = content[0].split(';');
        content.shift();

        content.forEach((item)=>{
            var temp = item.replace(",").split(';');
            var data = {}
            for(var i=0;i<key.length;i++){
                data[key[i]] = temp[i]
            }

            jsonData.push(data);
        })

        jsonData.sort(function(a, b) {
            return compareStrings(a.title, b.title);
        })

    // console.log is working fine but when I tried to return the same data it won't work properly

        // console.log(jsonData);
        callback(null, jsonData);
    })
}

csv_to_json((error, data) => {
  console.log(data);
})
Pooya
  • 2,968
  • 2
  • 12
  • 18
  • well, it's again consoling out the data, but I want to store the data in a variable when we assign it to. example:- **var my_data = csv_to_json()** so for that the function should return the data but it won't – Markus nuan Nov 14 '21 at 13:58
  • First better read how nodejs callback work, you can't store variable like `var my_data = csv_to_json()` if your function was callback, I suggest you to use async/await it is easy to store variable `var my_data = await csv_to_json()` – Pooya Nov 15 '21 at 08:09