我在server/statusboard.js中有以下代碼;當調用服務器上的Collection.insert時,「流星代碼必須始終運行在光纖內」
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提要中提取一些數據並嘗試將其推送到集合中。
當我啓動流星我得到以下異常;
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
我不太確定那個錯誤的含義。有沒有人有任何想法,或可以建議一個不同的方法?
我要清楚的是「服務」已在別處定義在一個單獨的文件(兩者共同的客戶:
,如果你想知道更多的關注於事件觸發考慮到這些視頻和服務器)。 – 2012-04-17 14:18:49
這條線是問題: '服務。insert({host:host,service:service,hardState:hardState,currState:currState});' 我認爲它是因爲它在回調中,暫時沒有辦法測試你。 – jonathanKingston 2012-04-17 14:31:24
流星現在包括一個HTTP請求庫,使您的情況更容易:http://docs.meteor.com/#meteor_http – debergalis 2012-05-06 16:13:04