【问题标题】:How to run Meteor insert within a Fiber如何在光纤中运行 Meteor insert
【发布时间】:2016-04-18 16:21:52
【问题描述】:

注意:关于这个主题有很多问题,但我无法将我的代码(由于语法问题等)转换为正确的格式。所以这是一个关于我的具体实例的问题。

我在“lib”文件夹下的 init.js 中有一些代码,用于从 web3/ethereum 包中获取事件并将其存储在集合中。请注意,这个问题纯粹是一个流星问题。我已经包含了有关 web3/ethereum 的上下文信息,因为这可能是其他人在 Meteor 集合中存储 web3/ethereum 事件时遇到的类似问题。

var events = contract_instance.allEvents([]);

events.watch(function(error, event){
  if (!error)
    console.log(event.args);

var event_object_value1 = event.args.value1;
//everything up to this point works fine.  event_object is in a json format.

//inserting the value into a collection on the server side like this is what causes the error.
collection.insert({"key": value1});
});

最后一行产生Meteor code must always run within a Fiber.error。通常,我会使用 Meteor 方法插入,但我怀疑在实际插入时会遇到同样的错误。

在 stackoverflow 上有很多关于这个错误的问题,但我还没有真正能够正确地将我的代码放入纤程中。我试图在这里遵循这个例子,但我认为这只是超越了我对 Meteor 的理解: https://meteorhacks.com/fibers-eventloop-and-meteor/

【问题讨论】:

    标签: javascript meteor ethereum


    【解决方案1】:

    只需替换

    events.watch(function(error, event) {
      ...
    });
    

    events.watch(Meteor.bindEnvironment(function(error, event) {
      ...
    }));
    

    Meteor.bindEnvironment 确保被包装的函数在纤程内运行。

    【讨论】:

      猜你喜欢
      • 2013-07-30
      • 2013-06-16
      • 2016-11-27
      • 2015-08-22
      • 2015-03-28
      • 1970-01-01
      • 2016-12-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多