Using node-csv and meteor-file to import CSV into a collection

前端 未结 2 2174
栀梦
栀梦 2020-12-29 16:15

Been struggling for several hours now trying to import CSV, uploaded from client using meteor-file and converted to CSV using node-csv serv

2条回答
  •  感情败类
    2020-12-29 17:02

    So it turns out because CSV() uses a callback and runs Asynchronous code, I needed to use a 'Future'. For more explaination see http://gist.io/3443021

    here's my working code:

    Meteor.methods({
         'uploadFile': function (file) {
    
          Future = Npm.require('fibers/future');
    
          console.log(file.name+'\'+file.type+'\'+file.size);                       
    
          file.save('/home/russell/tmp',{});
          var buffer = new Buffer(file.data);
    
    
          // Set up the Future
          var fut = new Future(); 
    
          // Convert buffer (a CSV file) to an array
          CSV().from(
                       buffer.toString(),
                       {comment: '#', delimiter: ',', quote: ''} 
                    )
               .to.array( function(data){
    
                                           var newRecords=[];
    
                                           for(var row=0; row

    });

提交回复
热议问题