gpt4 book ai didi

javascript - 如何将数据从 CSV 文件导入服务器端的 Meteor 集合

转载 作者:可可西里 更新时间:2023-11-01 09:11:25 25 4
gpt4 key购买 nike

我正在尝试为我之前的帖子找到解决方案: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-file 不再与当前版本的 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("%");
};

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

很明显我做错了什么,但我不知 Prop 体是什么。但是我认为解决方案并不太远。也许你们中有人可以告诉我正确的方向?

亲切的问候

编辑:

为了回复 revmen 的回答,这里是我的测试应用程序的完整代码:

测试.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>

测试.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 函数从客户端 block 移动到服务器 block 。

关于javascript - 如何将数据从 CSV 文件导入服务器端的 Meteor 集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29015201/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com