Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
31
rated 0 times [  37] [ 6]  / answers: 1 / hits: 20542  / 12 Years ago, tue, april 17, 2012, 12:00:00

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


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



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?


More From » meteor

 Answers
14

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.


[#86187] Monday, April 16, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
cristinadomoniquel

Total Points: 320
Total Questions: 94
Total Answers: 94

Location: Moldova
Member since Sat, Aug 6, 2022
2 Years ago
cristinadomoniquel questions
Wed, Apr 7, 21, 00:00, 3 Years ago
Tue, Dec 1, 20, 00:00, 4 Years ago
Mon, Nov 23, 20, 00:00, 4 Years ago
Mon, Aug 17, 20, 00:00, 4 Years ago
;