将csv文件内容从angular js传递到node js,并在node js中解析以插入mongo db

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

本文关键字:js node 并在 db mongo 插入 文件 csv angular      更新时间:2023-09-26

我有这个ReadCSV.js,我从angular js 中检索csv文件内容

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>

我得到的值是未定义的。如何将csv内容从angular js传递到node js并解析它,以便我可以插入mongodb。

试试下面的代码,注意我们使用的是这个>$scope,Angular文档建议这样做。

我过去一直在使用json文件,所以我不一定知道这是否适用于csv文件。

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

从csv/json文件检索数据后,应该在li中使用ngRepeat来显示要在html正文部分中显示的数据

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

我希望这能有所帮助,祝你好运。