[英]How can I insert data to my collection on my server-side main.js with meteor?
[英]How to import data from CSV file into Meteor collection at server side
我正在嘗試為我之前的帖子找到解決方案: Mongo在Meteor應用程序中的_id_字段上給出了重復的鍵錯誤
為了做到這一點,我想從服務器端的CSV文件中讀取數據,而不是從客戶端讀取數據。
首先,我在本文中嘗試使用node-csv和meteor-file將CSV導入集合,但meteor-file與當前版本的Meteor不再兼容。 我也嘗試過這個帖子上傳數據到Meteor / Mongo數據庫的解決方案,但它也在客戶端上,該解決方案產生的錯誤與我之前的帖子相同。
經過一些進一步的研究后,我嘗試用下面的代碼讀取數據。 但它不起作用:
首先我創建了一個集合:
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("%");
};
當我嘗試讀取文件時,沒有任何反應。 只有控制台日志出現在服務器控制台中,但沒有導入任何內容。 甚至沒有創建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
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");
}
});
}
你非常接近。 我只需做一些改動就可以了。
我不知道你的.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函數從客戶端塊移動到服務器塊。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.