CSVto json client side

30 Views Asked by At

I want to connect my csvtojson function to html(client side) but i'm getting some errors. Should i have a listener to the input file ? One of the errors is require not define. But i have a package.json with require on it

Html code

<!DOCTYPE html>
<html lang="en">
<head>  
    <p>Convert CSV to JSON.</p>
<title>Swim API</title>
</head>
<script src='node.js' type='text/javascript'></script>
<body>
    <p>Choose a csv to upload</p>
    <input name="myFile" type="file" id="getBtn">   
    <button id="myBtn">Upload</button>  
</body>
</html>

script.js

var x = document.getElementById("myBtn");
x.addEventListener("click", myFunction);
const csvtojsonV2=require('csvtojson')
function myFunction(){   
    csvtojsonV2({        
        noheader:true,
        checkType:true,    
        headers:["Time","Yaw","Pitch","Roll","Heading","Ax", "Ay", "Az","Gx","Gy", "Gz", "Mx", "My", "Mz"],       
                })
    .fromFile('getBtn')
    .then((json) => {
       const RESULT = json.reduce((acc, v, i) => {
          for (const [key, value] of Object.entries(v)) {
            if (!acc[key]) acc[key] = [];
            acc[key].push(value);
          }
          return acc;
        }, {});
       console.log(RESULT)  
        }) 
    
  }

Package.json

{
    "name": "requirejs",
    "license": "MIT",
    "volo": {
      "url": "https://raw.github.com/requirejs/requirejs/{version}/require.js"
    },
    "main": "require.js",
    "scripts": {
      "pretest": "jscs . && jshint require.js"
    },
    "repository": {
      "type": "git",
      "url": "https://github.com/requirejs/requirejs.git"
    },
    "devDependencies": {
      "jscs": "^1.12.0",
      "jshint": "^2.7.0"
    }
  }
0

There are 0 best solutions below