【问题标题】:Pass csv file content from angular js to node js and parse in node js to insert in mongo db将 csv 文件内容从 angularjs 传递到节点 js 并在节点 js 中解析以插入 mongo db
【发布时间】:2016-08-08 09:53:18
【问题描述】:

我有这个 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。

【问题讨论】:

    标签: javascript angularjs node.js mongodb csv


    【解决方案1】:

    试试下面的代码,注意我们正在使用这个 > $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>
    

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

    【讨论】:

      猜你喜欢
      • 2017-03-15
      • 1970-01-01
      • 2016-03-14
      • 2020-01-13
      • 1970-01-01
      • 2016-08-03
      • 1970-01-01
      • 1970-01-01
      • 2018-10-15
      相关资源
      最近更新 更多