如何在光纤中运行Meteor插件

How to run Meteor insert within a Fiber

本文关键字:运行 Meteor 插件 光纤      更新时间:2023-09-26

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

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

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.错误。通常,我会使用Meteor方法进行插入,但我怀疑在实际插入时也会出现同样的错误。

关于stackoverflow上的这个错误,有很多问题,但我还没能在光纤中正确地获取代码。我试着在这里以这个例子为例,但我认为这远远超出了我对流星的理解:https://meteorhacks.com/fibers-eventloop-and-meteor/

只需更换

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

带有

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

Meteor.bindEnvironment确保被包裹的函数在光纤中运行。