How to import data from CSV file into Meteor collection at server side

前端 未结 1 1545
谎友^
谎友^ 2021-02-03 15:30

I\'m trying to find a solution for my previous post: Mongo gives duplicate key error on _id_ field in Meteor application

In order to do that I want to read data from my

相关标签:
1条回答
  • 2021-02-03 16:26

    You were very close. I just had to make a few changes to get it working.

    I don't know what your .csv file looks like, so I made one that's like this:

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

    Your file.split operation wasn't splitting the lines, but was putting everything on one big line. I did it this way and it worked:

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

    That got individual lines to split into members of the array. Then I assumed that, since you're calling your input a CSV, your values are separated by commas, not pipes. So I changed your line.split to this

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

    Other changes I made may not be what was causing yours to fail, but this is how I think things are normally done...

    Instead of declaring your collection like this

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

    I did it like this

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

    Note that this is run by both the server and the client.

    Instead of your way of declaring methods on the server, I just put this into server code (not in Meteor.start):

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

    And, of course, I changed the insert line at the bottom of your import_file_orders function

    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));
    

    EDIT for updated code in the question:

    Move the import_file_orders function from the client block to the server block.

    0 讨论(0)
提交回复
热议问题