What does the following error message mean?
fence has already activated -- too late to add writes
Here's 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"
Once the method is done executing you can't add additional writes. To delay completing the methods you can use Futures. Something like this:
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);
}
});
Methods have to finish all their writes before they return.
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 this is using Futures like avital says. If you want to do multiple callbacks in parallel or other complex things, you can use the Futures api. However, if you are just making one request, or your requests already have to be in sequence, using the synchronous version of Meteor.http.get
works and is easier to type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With