“Meteor 代码必须始终在 Fiber 中运行"在服务器上调用 Collection.insert 时 [英] "Meteor code must always run within a Fiber" when calling Collection.insert on server

查看:8
本文介绍了“Meteor 代码必须始终在 Fiber 中运行"在服务器上调用 Collection.insert 时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 server/statusboard.js 中有以下代码;

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

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


function getServices(services) {
  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});
    });
  });
}

Meteor.startup(function () {
  var services = [];
  getServices(services);
  console.log(services);
});

基本上,它从 JSON 提要中提取一些数据并尝试将其推送到集合中.

Basically, it's pulling some data from a JSON feed and trying to push it into a collection.

当我启动 Meteor 时,出现以下异常;

When I start up Meteor I get the following exception;

app/packages/livedata/livedata_server.js:781
      throw exception;
            ^
Error: Meteor code must always run within a Fiber
    at [object Object].withValue (app/packages/meteor/dynamics_nodejs.js:22:15)
    at [object Object].apply (app/packages/livedata/livedata_server.js:767:45)
    at [object Object].insert (app/packages/mongo-livedata/collection.js:199:21)
    at app/server/statusboard.js:15:16
    at Array.forEach (native)
    at Function.<anonymous> (app/packages/underscore/underscore.js:76:11)
    at Request._callback (app/server/statusboard.js:9:7)
    at Request.callback (/usr/local/meteor/lib/node_modules/request/main.js:108:22)
    at Request.<anonymous> (/usr/local/meteor/lib/node_modules/request/main.js:468:18)
    at Request.emit (events.js:67:17)
Exited with code: 1

我不太确定那个错误是什么意思.有没有人有任何想法,或者可以提出不同的方法?

I'm not too sure what that error means. Does anyone have any ideas, or can suggest a different approach?

推荐答案

如上所述,这是因为您在回调中执行代码.

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

您在服务器端运行的任何代码都需要包含在 Fiber 中.

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

尝试将您的 getServices 函数更改为如下所示:

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.

您可能可以只将插入语句包装在 Fiber 中,但我并不肯定.

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

这篇关于“Meteor 代码必须始终在 Fiber 中运行"在服务器上调用 Collection.insert 时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆