流星纤维问题?

时间:2013-11-21 04:44:20

标签: javascript mongodb coffeescript meteor

我正在提取CSV,将其解析为数组(效果很好)并尝试填充Mongo Collection(失败)。

我在终端中收到以下错误:“错误:Meteor代码必须始终在Fiber中运行。尝试使用Meteor.bindEnvironment包装传递给非Meteor库的回调。”

这是我的JS代码(服务器):

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

如果你如此倾向,你也可以看到CoffeeScript:

  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]
                })

我正在尝试使用下面提到的Meteor bindEnvironment。这是当前的状态:

我使用用户触发的模板事件调用服务器方法:

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

在服务器上,方法如下:

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

1 个答案:

答案 0 :(得分:0)

解决方案是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);
        }
    )
);