0

I am playing around with some nodeJs things, I read about block and functions scooping but I was not able to access my variable jsonArrayObj:

csv({delimiter: ['   ']})
  .fromFile(csvFilePath)
  .on('end_parsed', function(jsonArrayObj) {
    console.log(jsonArrayObj);
    function cssResult() {
      return jsonArrayObj;
    }
  })
  .on('error', (err) => {
    console.log(err);
  });

console.log('jsonArrayObj'); // I want to print the results here also

2 Answers 2

1

You can do it something like this.

csv({delimiter: ['   ']})
.fromFile(csvFilePath)
.on('end_parsed', function(jsonArrayObj) {
 console.log(jsonArrayObj);
 myNewFunc(jsonArrayObj);
 function cssResult() {
  return jsonArrayObj;
 }
})
.on('error', (err)=>{
 console.log(err);
});

var myNewFunc = function(myVar){
  console.log(myVar); // this will be your result
};
Sign up to request clarification or add additional context in comments.

Comments

0

Your variable jsonArrayObj is just defined on your function's scope. So jsonArrayObj is just accessible in your function.

If you want to use it out of your function, you have to define a variable outside of your function, and then tell your function that this variable takes jsonArrayObj's value.

Like this :

 var myRes;
 csv({delimiter: ['   ']})
      .fromFile(csvFilePath)
      .on('end_parsed', function(jsonArrayObj) {
        myRes = jsonArrayObj;
        console.log(jsonArrayObj);
        function cssResult() {
          return jsonArrayObj;
        }
      })
      .on('error', (err)=>{
        console.log(err);
      });

    console.log(myRes); 

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.