“fence has already activated — too late to add writes”

拥有回忆 提交于 2019-12-22 09:38:34

问题


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:

  • Mac OS X Lion
  • Meteor 0.3.8

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"

回答1:


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);
  }
});



回答2:


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.



来源:https://stackoverflow.com/questions/11485904/fence-has-already-activated-too-late-to-add-writes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!