"fence already activated - too late to add entries"

What does the following error message mean?

fence is already activated - it's too late to add entries

Here is an example of how to get it:

Environment :

Project Creation :

meteor create test cd test meteor add coffeescript http mv test.js test.coffee meteor 

test.coffee :

 Records = new Meteor.Collection("records") if Meteor.is_client Meteor.startup -> Meteor.call "test" if Meteor.is_server Meteor.methods test: -> Meteor.http.get "http://www.meteor.com", -> Records.insert some:"data" 
+4
source share
2 answers

After executing the method, you cannot add additional entries. To delay the execution of methods, you can use Futures. Something like that:

 Meteor.methods({ foo: function() { var futures = _.map(urls, function(url) { var future = new Future(); var onComplete = future.resolver(); Meteor.http.get(url, function(error, result) { // do whatever you need onComplete(); }); return future; }); Future.wait(futures); } }); 
+6
source

Methods must finish all of their entries before returning.

In this example, the easiest way would be to simply omit the callback and use the return value of Meteor.http.get:

 if Meteor.is_server Meteor.methods test: -> data = Meteor.http.get "http://www.meteor.com" Records.insert some:"data" 

Behind the scenes, it uses futures such as avital. If you want to execute multiple callbacks in parallel or other complicated things, you can use futures api. However, if you just make one request or your requests should already be consistent, using the synchronous version of Meteor.http.get works and is easier to enter.

+4
source

All Articles