Hi there. I’m currently new in meteor, and to be honest, i’m not pretty experienced into javascript as well
So I come here to get an advice about the best practice to get call method on the server, getting the callback etc…
Here is an example on i’m pretty stuck :
client.js : This is just a simple event listenner on a button’s click
Template.terminalHS6.events({
'click #myBtn': function (e, obj) {
Meteor.call("processRequest", function (err, data) {
if(err){
console.log("err : " + err);
}else{
console.log("data : " + data);
}
});
}
});
server.js :
var http = Npm.require("http");
Meteor.methods({
"processRequest": function () {
var response = {}; // return var
// option http who seems like this : http://localhost:8091/telnet/?type=users
var options = {
host: 'localhost',
port: 8091,
path: '/telnet/?type=users',
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
};
// asynchronous http request
var prot = http;
var req = prot.request(options, function(res)
{
var output = '';
console.log(options.host + ':' + res.statusCode);
res.setEncoding('utf8');
res.on('data', function (chunk) {
output += chunk;
});
return res.on('end', function(err) {
var obj = JSON.parse(output); // my json seems like : {"threads":[],"type":"text"}
return obj; // my obj seems like : {array[0] threads, "type"="text"}
// until there... all is fine !
});
});
req.on('error', function(err) {
res.send('error: ' + err.message);
});
req.end();
}
});
SO… when I’m getting my request’s result in the res.on code part, obviously, asynchronously () the result i get on the client side is data = undefined.
I tried to separate all my code into different functions to do like “true” javascript, to emule synchronous call and getting my result properly… but it’s still doesn’t works !
How can I do this clearly in meteor ?
Thx for your help, ideas, discussion about