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

时间:2015-03-12 16:28:00

标签: javascript mongodb 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
  onLoadCallback(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("%");
};

当我尝试阅读文件时,没有任何反应。只有控制台日志出现在服务器控制台中,但没有导入任何内容。即使订单集合也没有创建。

很明显,我做错了什么,但我不知道到底是什么。但是我认为解决方案并不是太过分。也许有人可以告诉我正确的方向?

亲切的问候

编辑:

为了回答这里的答案,我的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
  onLoadCallback(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");
 }
});

}

1 个答案:

答案 0 :(得分:6)

你非常接近。我只需做一些改动就能让它发挥作用。

我不知道你的.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函数从客户端块移动到服务器块。