0

I am pulling a CSV, parsing it into an Array (works great) and attempting to populate a Mongo Collection (fails).

I am getting the following error in the terminal: "Error: Meteor code must always run within a Fiber. Try wrapping callbacks that you pass to non-Meteor libraries with Meteor.bindEnvironment."

Here is my JS Code (Server):

var order, orders, _i, _len;

CSV().from(fileAddress).to.array(function(data) {});

orders = data;

for (_i = 0, _len = orders.length; _i < _len; _i++) {
  order = orders[_i];
  Orders.insert({
    clientId: order[0],
    shipmentId: order[1],
    orderId: order[2],
    orderDate: order[3],
    expectedDeliveryDate: order[4],
    shipMethod: order[5],
    customerName: order[6],
    companyName: order[7],
    address1: order[8],
    address2: order[9],
    city: order[10],
    state: order[11],
    zipCode: order[12],
    country: order[13],
    itemId: order[14],
    quantity: order[15],
    sku: order[16],
    itemDescription: order[17],
    productVariationDetails: order[18],
    artfile: order[19],
    customerMessage: order[20]
  });
}

If you are so inclined, you can see the CoffeeScript as well:

  CSV().from(fileAddress).to.array (data) ->
        orders = data
        for order in orders
            Orders.insert({
                clientId: order[0],
                shipmentId: order[1],
                orderId: order[2],
                orderDate: order[3],
                expectedDeliveryDate: order[4],
                shipMethod: order[5],
                customerName: order[6],
                companyName: order[7],
                address1: order[8],
                address2: order[9],
                city: order[10],
                state: order[11],
                zipCode: order[12],
                country: order[13],
                itemId: order[14],
                quantity: order[15],
                sku: order[16],
                itemDescription: order[17],
                productVariationDetails: order[18],
                artfile: order[19],
                customerMessage: order[20]
                })

I am trying to use the Meteor bindEnvironment noted below. Here is the current state:

I am calling a server method using a user triggered template event:

Template.upload.events
    'click #runMethod': () ->
        Meteor.call('csv2Array', Session.get("fileUrl"), (error, result) ->
            console.log result[0])

On the server, the method is as follows:

Meteor.methods
    'csv2Array': (fileUrl) ->
        data = CSV().from(fileUrl).to.array Meteor.bindEnvironment((data) ->
            console.log data[0], (error) ->
                console.log "Error in bindEnvironment", error)

When I run the method, I get this error:

I2047-12:18:10.287(-8)? Exception while invoking method 'csv2Array' Error: onException must be supplied
I2047-12:18:10.287(-8)?     at Object.Meteor.bindEnvironment (packages/meteor/dynamics_nodejs.js:65)
I2047-12:18:10.288(-8)?     at Meteor.methods.csv2Array (./csv.coffee:33:39)
I2047-12:18:10.288(-8)?     at maybeAuditArgumentChecks (packages/livedata/livedata_server.js:1356)
I2047-12:18:10.288(-8)?     at packages/livedata/livedata_server.js:541
I2047-12:18:10.288(-8)?     at _.extend.withValue (packages/meteor/dynamics_nodejs.js:35)
I2047-12:18:10.288(-8)?     at packages/livedata/livedata_server.js:540
I2047-12:18:10.289(-8)?     at _.extend.withValue (packages/meteor/dynamics_nodejs.js:35)
I2047-12:18:10.289(-8)?     at _.extend.protocol_handlers.method (packages/livedata/livedata_server.js:539)
I2047-12:18:10.289(-8)?     at packages/livedata/livedata_server.js:439
ppedrazzi
  • 787
  • 2
  • 10
  • 23
  • Does `Meteor.bindEnvironment` solves your problem ? – Kuba Wyrobek Nov 22 '13 at 05:25
  • @parhelium, I think it's the right solution, but I cant seem to get it to work. I will amend the question above with my simplified code. – ppedrazzi Nov 23 '13 at 20:20
  • All I am really trying to do is take a file URL from the client, convert it to an array on the server, and pass it back to the client. Should be straightforward, but the CSV (3rd party library) seems to be causing issues for me. – ppedrazzi Nov 23 '13 at 20:32

1 Answers1

0

Solution is Meteor.bindEnvironment:

var order, orders, _i, _len;

CSV().from(fileAddress).to.array(
    Meteor.bindEnvironment(
        function(data) {
            orders = data;
            for (_i = 0, _len = orders.length; _i < _len; _i++) {
              order = orders[_i];
              Orders.insert({
                clientId: order[0],
                //...
                customerMessage: order[20]
              })
            }
        }, 
        function(error) {
            console.log('Error in bindEnvironment:', error);
        }
    )
);
Kuba Wyrobek
  • 5,273
  • 1
  • 24
  • 26
  • Wow! That's this `Meteor.bindEnvironment` routine? I cant see it in the documentation :/ – Tomasz Lenarcik Nov 21 '13 at 09:38
  • Why not use standard `Fiber` wrapper? – Tomasz Lenarcik Nov 21 '13 at 09:40
  • It is very likely that `Fiber` wrapper should work in this situation because `The function returned from Meteor.bindEnvironment also automatically gets run in a Fiber.` However I read error message (use Meteor.bindEnvironment) and apply this solution knowing that it works [here](http://stackoverflow.com/questions/19314200/using-node-csv-and-meteor-file-to-import-csv-into-a-collection). @Tarang explained usage of Meteor.bindEnvironment [here](http://stackoverflow.com/questions/19994951/whats-going-on-with-meteor-and-fibers-bindenvironment) – Kuba Wyrobek Nov 21 '13 at 10:24
  • Chris Mather created screencast [What is Meteor.bindEnvironment](https://www.eventedmind.com/feed/49CkbYeyKoa7MyH5R). – Kuba Wyrobek Nov 21 '13 at 10:27