简体   繁体   中英

Pass csv file content from angular js to node js and parse in node js to insert in mongo db

I have this ReadCSV.js where i am retrieving csv file contents from angular js

app.get('/index.html', function(req, res) {
   res.sendFile(__dirname + '/index.html');
});

app.post('/add_event', function(req, res, next) {
    console.log("Node JS File1 :" +req);
    var finalData = req.body.test;//value is undefined
    console.log("Node JS File : " + finalData);//value is undefined

index.html

  var myapp = angular.module('myapp', []);

 myapp.controller('MainCtrl', function ($scope,$http) {
$scope.showContent = function($fileContent){
    $scope.content = $fileContent;
var test=       $fileContent;
alert(test);
};  

$scope.submit = function(test){
//  
 $http.post('/add_event', $fileContent)
        .success(function(){
            console.log( "here" +$fileContent);
    });
}


<body ng-controller="MainCtrl">
<form ng-submit="submit()">
<div class="test">
<div class="panel-heading">
    <h3 class="panel-header">Upload Events</h3>
</div>
  <input class="btn btn-primary" type="file" on-read-     
  file="showContent($fileContent)"/></br>
  <button class="btn btn-primary btn-block" ng-click="processData(content)"   
  type="submit">
    Upload File
  </button>
  </div>
 </form>
  <div class="test2">
   {{content}}
   </div>

I am getting value as undefined. How to pass the csv content from angular js to node js and parse it so that i can insert in mongodb.

Try the below code, note we are using this > $scope, the Angular documentation suggests doing so.

I've been using json files in the past, so i don't necessarily know if this will work with a csv file.

MyApp.query(); 
    angular.
      module('myApp').
        controller: ['MyApp',
         function MyAppController(MyApp) {
            this.myApp = MyApp.query();
          }
        ]
      });

After you retrieve the data from the csv/json file, you should use an ngRepeat in an li to display the data you want to display in the html body section

    <li ng-repeat="data in $ctrl.myApp"
    <p>{{myApp.data}}</p>
   </li>

I hope this helps somehow, best of luck with it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM