Skip to main content

iI am working on a Reactjs Project ,in.In the project iI have scenario like iI need to read a csv file from local and convert it into json objects.i I am using csvtojson node package for converting csv files to json objects.see See the below code below:

import React from 'react';
import ReactDOM from 'react-dom';

var objects;

var ReadFile = React.createClass({
    readFile:function(){
        var file = this.refs.file.files[0];
        var reader = new FileReader();
        reader.onload = function(evt){
             var resultText = evt.target.result;
             objects = this.csvToJson(resultText);
             console.log(objects);
        }.bind(this);
        var newFile = file.slice(0,5000);
        reader.readAsText(newFile); 
    },
    csvToJson:function(csvString){
        var Converter = require("csvtojson").Converter;
        var converter = new Converter({});
        converter.fromString(csvString, function(err,result){
           //When i console log the result it is working but when i return theThe result :
           // i am getting a undefined error
           //console.log(result);
           return result;
        });
    },
    render:function(){
      return (
         <input type="file" ref="file" onChange={this.readFile} /> 
      );
    }
});

ReactDOM.render(<ReadFile />,document.getElementById('container'));

When iI console log the result from the csvToJson file iI am getting a undefined error.please Please somebody guide me, what iI am doing wrong?

i am working on a Reactjs Project ,in the project i have scenario like i need to read a csv file from local and convert it into json objects.i am using csvtojson node package for converting csv files to json objects.see the below code

import React from 'react';
import ReactDOM from 'react-dom';

var objects;

var ReadFile = React.createClass({
    readFile:function(){
        var file = this.refs.file.files[0];
        var reader = new FileReader();
        reader.onload = function(evt){
             var resultText = evt.target.result;
             objects = this.csvToJson(resultText);
             console.log(objects);
        }.bind(this);
        var newFile = file.slice(0,5000);
        reader.readAsText(newFile); 
    },
    csvToJson:function(csvString){
        var Converter = require("csvtojson").Converter;
        var converter = new Converter({});
        converter.fromString(csvString, function(err,result){
           //When i console log the result it is working but when i return the result 
           // i am getting a undefined error
           //console.log(result);
           return result;
        });
    },
    render:function(){
      return (
         <input type="file" ref="file" onChange={this.readFile} /> 
      );
    }
});

ReactDOM.render(<ReadFile />,document.getElementById('container'));

When i console log the result from the csvToJson file i am getting a undefined error.please somebody guide me what i am doing wrong

I am working on a Reactjs Project .In the project I have scenario like I need to read a csv file from local and convert it into json objects. I am using csvtojson node package for converting csv files to json objects. See the code below:

import React from 'react';
import ReactDOM from 'react-dom';

var objects;

var ReadFile = React.createClass({
    readFile:function(){
        var file = this.refs.file.files[0];
        var reader = new FileReader();
        reader.onload = function(evt){
             var resultText = evt.target.result;
             objects = this.csvToJson(resultText);
             console.log(objects);
        }.bind(this);
        var newFile = file.slice(0,5000);
        reader.readAsText(newFile); 
    },
    csvToJson:function(csvString){
        var Converter = require("csvtojson").Converter;
        var converter = new Converter({});
        converter.fromString(csvString, function(err,result){
           //When i console log the result it is working but when i return The result:
           // i am getting a undefined error
           //console.log(result);
           return result;
        });
    },
    render:function(){
      return (
         <input type="file" ref="file" onChange={this.readFile} /> 
      );
    }
});

ReactDOM.render(<ReadFile />,document.getElementById('container'));

When I console log the result from the csvToJson file I am getting a undefined error. Please somebody guide me, what I am doing wrong?

Source Link
yasar
  • 2.8k
  • 5
  • 21
  • 18

Read a File through FileReader Api and Convert it into json object

i am working on a Reactjs Project ,in the project i have scenario like i need to read a csv file from local and convert it into json objects.i am using csvtojson node package for converting csv files to json objects.see the below code

import React from 'react';
import ReactDOM from 'react-dom';

var objects;

var ReadFile = React.createClass({
    readFile:function(){
        var file = this.refs.file.files[0];
        var reader = new FileReader();
        reader.onload = function(evt){
             var resultText = evt.target.result;
             objects = this.csvToJson(resultText);
             console.log(objects);
        }.bind(this);
        var newFile = file.slice(0,5000);
        reader.readAsText(newFile); 
    },
    csvToJson:function(csvString){
        var Converter = require("csvtojson").Converter;
        var converter = new Converter({});
        converter.fromString(csvString, function(err,result){
           //When i console log the result it is working but when i return the result 
           // i am getting a undefined error
           //console.log(result);
           return result;
        });
    },
    render:function(){
      return (
         <input type="file" ref="file" onChange={this.readFile} /> 
      );
    }
});

ReactDOM.render(<ReadFile />,document.getElementById('container'));

When i console log the result from the csvToJson file i am getting a undefined error.please somebody guide me what i am doing wrong