2016-04-18 25 views
1

注意:關於此主題有許多問題,但我無法將我的代碼(由於語法問題等原因)轉換爲正確的格式。所以這是一個關於我的具體實例的問題。如何在光纖中運行Meteor插入

我在「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.錯誤。通常情況下,我會使用流星方法插入,但我懷疑在實際插入時會出現同樣的錯誤。

在stackoverflow上有關於此錯誤的問題有很多,但我沒有真正能夠正確地在光纖中獲得我的代碼。我一直在努力,在這裏按照這個例子,但我認爲這只是方式我上面的流星的理解: https://meteorhacks.com/fibers-eventloop-and-meteor/

回答

2

只需更換

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

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

Meteor.bindEnvironment保證包裝的功能在光纖中運行。