Uploading CSV to meteor: _id not defined

241 views Asked by At

I'm trying to create a meteor app that can upload a CSV and create JSON objects from it. I closely followed this tutorial, but my code doesn't work. The console tells me that _id is not defined in some client code where I am calling a Meteor method. I need help figuring out why this is happening and, more broadly, why the code won't work properly even though I followed the tutorial.

Any help on this is greatly appreciated, including other solutions for uplaoding CSV files to meteor.

The parts of my code that (as far as I can tell) are relevant:

home.html:

<template name="home">
  <div class="template-home">
    <div class="page-header">
      <h1>CSV Importer</h1>
    </div>
  
 <div class="container">
   <div class="row">
    <p>
     <button type="button" class="btn btn-default btn-file">
     Import CSV <input type="file" name="myFileInput" class="myFileInput" value="">
     </button>
    </p>
    .............
</template>

home.js:

Template.home.events({
 "change .myFileInput": function(evt, templ){
  FS.Utility.eachFile(event, function(file){
   var theFile = new FS.File(file);
   Uploads.insert(theFile, function(err,fileObj){
    if(!err){
     Meteor.call('uploadFile', fileObj,_id,file,name);
    }
   })
  })
 }
});

upload.js (where the method is defined):

Meteor.methods({
 'uploadFile':function(fileid,filename){
  var fs = Meteor.npmRequire('fs');
  var file = Uploads.find({_id:fileid});
  Meteor.setTimeout(function(){
   var filepath = '/imports/uploads-' + '-' + filename;
   CSV().from.stream(
   fs.createReadStream(filepath),
   {'escape':'\\'})
   .on('record', Meteor.bindEnvironment(function(row,index){
    Address.insert({
     'Factor':row[0],
     'Caracteristica':row[1],
     'Number':row[2],
     'Indicador':row[3],
     'Razon':row[4],
     'UA':row[5],
     'UC':row[6],
     'UM':row[7],
     'UCe':row[8],
     'UCo':row[9],
     'US':row[10],
     'Tot':row[11],
     'Color':row[12],
    })
   }, function(error){
    console.log(error);
   }))
   .on('error', function(err){
    console.log(err);
   })
   .on('end', function(count){
    
   })
  },1000)
 }
})

Thanks

1

There are 1 answers

3
Billybobbonnet On BEST ANSWER

I see several errors in your code.

  1. your _id is not defined in the code you posted, and if it was, I assume it wouldn't be named like that. It would be a key attached to a document, like item._id or maybe in your case fileObj._id.
  2. You call your method with 4 parameters when it only has 2

The obvious mistake to me is that the line where you call your method should be

Meteor.call('uploadFile', fileObj._id,file.name);

with dots instead of comma.

You also might want to add a callback to your method call. You can write it like this:

    Meteor.call("meteorMethod", dataObject, function(error, result){ 
        if(error){ 
            console.log("error", error); 
        } 
        if(result){ 

        } 
    });