0

I do not know what should be the problem but I currently implemented function that convert csv file to JSON and print result into console.log

function looks like :

var Converter = require("csvtojson").Converter;
var converter = new Converter({delimiter: ';'});

function convertToJSON() {

   converter.fromFile('data.csv', function(err, result) {

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

    var data = result;
    console.log(result);

    });
}

and when I call it this way in server.js it return JSON to console

convertToJSON();

but when I would like to call this function from app.get by REST GET it return allways empty object.

app.get("convertToJSON", function(req,res){
    convertToJSON();
})

I dont know what should be problem, why it is not work inside get call. There is no error during execution.

5
  • check the console and log the error, you might get an error in the console Commented Apr 2, 2018 at 12:26
  • no there is no error in console in nodeJS and no error in browser...in nodeJS console is only printed empty [ ] Commented Apr 2, 2018 at 12:28
  • 1
    your convertToJSON-Method doesn't return anything. It is just logging the object to console. Commented Apr 2, 2018 at 12:29
  • 1
    @rweisse is right you should implement a callback function for your method, so when you call it convertToJSON(function(error, data) { res.send(data); }) Commented Apr 2, 2018 at 12:31
  • I know that it is not return anything I need to make DB insert after geting parsed JSON and save it. So I dont need return in this function. But Why it log result when it call in serverJS during starting server and not after call this function from app.get() ?? Commented Apr 2, 2018 at 12:33

2 Answers 2

1

Your code has become asynchronous use a promise in that funtion. Your file is taking more time to load when called inside app.get.

function convertToJSON() {

var promise1 = new Promise(function(resolve, reject) {



 converter.fromFile('data.csv', function(err, result) {

   if (err) {
    reject(err);
   }

   var data = result;
   resolve(result);


   });
   });
return promise1;
}
Sign up to request clarification or add additional context in comments.

5 Comments

And how can I fix that ? I am not very familiar with AJAX and NodeJS ..
@strakz Check the example that I added
thank you but I dont get it, what is usefull and important from your example for me ..
I have modified your function you just need to use this promise function.
but it is not working, still return empty array :( so that is not problem
0

As we're using Promise, we need to handle it properly. Please take a look at this reference for more details.

Try this...

var Converter = require("csvtojson").Converter;
var converterObj = new Converter({delimiter: ';'});

function convertToJSON() {
    return new Promise((resolve, reject) => {
        converterObj.fromFile('data.csv', (err, result) => {
            if (err) {
                reject(err);
            }

            resolve(result);
        }); 
    });
}

convertToJSON()
    .then((result) => {
        console.log(result);
    })
    .catch((error) => {
        console.log(error);
    });

2 Comments

still got the same problem, This solution only works if I call it out of app.get(/some link) method and if I would like to call this method inside rest call, that the function should be called then it always return me empty array of JSON. so I dont know if there is problem to load file or read file or I dont know, but thanks for your time
Ok... What's the return type of function "converterObj.fromFile"? Would be good to have a bigger picture from the scope of your app. Regards.

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.