“Meteor code must always run within a Fiber” when calling Collection.insert on server

后端 未结 3 1785
无人共我
无人共我 2020-11-27 15:50

I have the following code in server/statusboard.js;

var require = __meteor_bootstrap__.require,
    request = require(\"request\")   


function getServices(         


        
3条回答
  •  失恋的感觉
    2020-11-27 16:15

    As mentioned above it is because your executing code within a callback.

    Any code you're running on the server-side needs to be contained within a Fiber.

    Try changing your getServices function to look like this:

    function getServices(services) {
      Fiber(function() { 
        services = [];
        request('http://some-server/vshell/index.php?type=services&mode=json', function (error, response, body) {
          var resJSON = JSON.parse(body);
           _.each(resJSON, function(data) {
             var host = data["host_name"];
             var service = data["service_description"];
             var hardState = data["last_hard_state"];
             var currState = data["current_state"];
             services+={host: host, service: service, hardState: hardState, currState: currState};
             Services.insert({host: host, service: service, hardState: hardState, currState: currState});
          });
        });
      }).run();  
    }
    

    I just ran into a similar problem and this worked for me. What I have to say though is that I am very new to this and I do not know if this is how this should be done.

    You probably could get away with only wrapping your insert statement in the Fiber, but I am not positive.

提交回复
热议问题