微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – 如何将CSV文件中的数据导入到服务器端的Meteor集合中

我正在尝试为我之前的帖子找到一个解决方案:Mongo gives duplicate key error on _id_ field in Meteor application

为了做到这一点,我想从服务器端的CSV文件中读取数据,而不是从客户端读取数据.

首先,我在Using node-csv and meteor-file to import CSV into a collection这个帖子中尝试了解决方案,但流星文件与Meteor的当前版本不再兼容.我也尝试过这篇文章Upload Data to Meteor / Mongo DB中的解决方案,但它也在客户端上,该解决方案会产生与我之前的帖子相同的错误.

经过一些进一步的研究后,我尝试用下面的代码读取数据.但它不起作用:

首先我创建了一个集合:

 Meteor.orders = new Meteor.Collection('Orders');

我定义了以下模板来读取csv文件:

<template name="read_file_orders">
  <form class="well form-inline">
   <label class="control-label" for="fileInput2">Kies bestand</label>
   <input class="input-file" id="fileInput2" type="file" name="files[]">
   <Button class="btn btn-primary" id="read_orders">Importeer</button>
   <button class="btn btn-danger" id="erase_orders">Wis gegevens</button>
  </form>
</template>

这是客户端javascript:

Template.read_file_orders.events({
 "click #read_orders" : function(e) {
  var f = document.getElementById('fileInput2').files[0];
  console.log("read file");
   readFile(f, function(content) {
    Meteor.call('upload',content);
   });
 }
});

readFile = function(f,onLoadCallback) {
 var reader = new FileReader();
 reader.onload = function (e){
  var contents=e.target.result
  onl oadCallback(contents);
 }
 reader.readAsText(f);
};

这是服务器javascript:

Meteor.startup(function () {
// code to run on server at startup

 return Meteor.methods({
  upload : function(fileContent) {
    console.log("start insert");
    import_file_orders(fileContent);
    console.log("completed");
  }
 });

});

import_file_orders = function(file) {
var lines = file.split('%\r\n');
var l = lines.length - 1;
for (var i=0; i < l; i++) {
  var line = lines[i];
  var line_parts = line.split('|');
  var ex_key = line_parts[0];
  var ex_name = line_parts[1];
  var clin_info = line_parts[2];
  var order_info = line_parts[3];
  var clinician_last_name = line_parts[4];
  var clinician_first_name = line_parts[5];
  var clinician_code = line_parts[6];
  var clinician_riziv = line_parts[7]
  var pat_id = line_parts[8];
  Meteor.orders.insert({Patient:pat_id, Exam_code:ex_key, Exam_name:ex_name, Clinical_info:clin_info, Order_info:order_info, Clinician_first:clinician_first_name, Clinician_last:clinician_last_name, Clinician_c_code:clinician_code, Clinician_riziv:clinician_riziv, Planned:null});
  console.log("%");
};

当我尝试读取文件时,没有任何反应.只有控制台日志出现在服务器控制台中,但没有导入任何内容.甚至没有创建Orders集合.

很明显,我做错了什么,但我不知道到底是什么.不过我认为解决方案并不太远.也许你们中的某个人可以向我展示正确的方向?

亲切的问候

编辑:

为了回复这里的答案,我的testapp的完整代码:

的test.html:

<head>
 <title>test</title>
</head>

<body>
 <h1>Welcome to Meteor!</h1>
 {{> read_file_orders}}
</body>

<template name="read_file_orders">
 <form class="well form-inline">
  <label class="control-label" for="fileInput2">Kies bestand</label>
  <input class="input-file" id="fileInput2" type="file" name="files[]">
  <Button class="btn btn-primary" id="read_orders">Importeer</button>
  <button class="btn btn-danger" id="erase_orders">Wis gegevens</button>
 </form>
</template>

test.js

Orders = new Mongo.Collection("orders");

if (Meteor.isClient) {
// counter starts at 0

Template.read_file_orders.events({
 "click #read_orders" : function(e) {
  var f = document.getElementById('fileInput2').files[0];
  console.log("read file");
  readFile(f, function(content) {
    Meteor.call('upload',content);
  });
 }
});

import_file_orders = function(file) {
 console.log("enter function import_file_orders")
 var lines = file.split(/\r\n|\n/);
 var l = lines.length - 1;
 for (var i=0; i < l; i++) {
  var line = lines[i];
  var line_parts = line.split(',');
  var ex_key = line_parts[0];
  var ex_name = line_parts[1];
  var clin_info = line_parts[2];
  var order_info = line_parts[3];
  var clinician_last_name = line_parts[4];
  var clinician_first_name = line_parts[5];
  var clinician_code = line_parts[6];
  var clinician_riziv = line_parts[7]
  var pat_id = line_parts[8];
  var result = Orders.insert({Patient:pat_id, Exam_code:ex_key, Exam_name:ex_name, Clinical_info:clin_info, Order_info:order_info, Clinician:{first:clinician_first_name, last:clinician_last_name, c_code:clinician_code, riziv:clinician_riziv}, Planned:null});
  console.log(Orders.findOne(result));
 };
}

readFile = function(f,onLoadCallback) {
//When the file is loaded the callback is called with the contents as a string
var reader = new FileReader();
reader.onload = function (e){
  var contents=e.target.result
  onl oadCallback(contents);
}
reader.readAsText(f);
};

}

if (Meteor.isServer) {
  Meteor.startup(function () {
  // code to run on server at startup

 });

 Meteor.methods({
   upload : function(fileContent) {
   console.log("start insert");
   import_file_orders(fileContent);
   console.log("completed");
 }
});

}

解决方法:

你非常接近.我只需做一些改动就可以了.

我不知道你的.csv文件是什么样的,所以我做了一个像这样的:

A1, B1, C1, D1, E1, F1, G1, H1, I1
A2, B2, C2, D2, E2, F2, G2, H2, I2

你的file.split操作没有分割线,但是把所有东西放在一个大线上.我是这样做的,它有效:

var lines = file.split(/\r\n|\n/);

这使得单独的行分裂成数组的成员.然后我假设,因为您将输入称为CSV,所以您的值用逗号分隔,而不是用管道分隔.所以我把你的line.split改成了这个

var line_parts = line.split(',');

我所做的其他改变可能不是导致你失败​​的原因,但这就是我认为通常做的事情……

而不是像这样宣布你的收藏

Meteor.orders = new Meteor.Collection('Orders');

我是这样做的

Orders = new Mongo.Collection("orders");

请注意,这由服务器和客户端运行.

我只是将它放入服务器代码(而不是Meteor.start)中,而不是在服务器上声明方法的方式:

Meteor.methods({
    upload : function(fileContent) {
        console.log("start insert");
        import_file_orders(fileContent);
        console.log("completed");
    }
});

当然,我更改了import_file_orders函数底部的插入行

var result = Orders.insert({Patient:pat_id, Exam_code:ex_key, Exam_name:ex_name, Clinical_info:clin_info, Order_info:order_info, Clinician_first:clinician_first_name, Clinician_last:clinician_last_name, Clinician_c_code:clinician_code, Clinician_riziv:clinician_riziv, Planned:null});
console.log(Orders.findOne(result));

编辑问题中的更新代码:

将import_file_orders函数从客户端块移动到服务器块.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐