Pradeep Saini Pradeep Saini - 3 months ago 25
Javascript Question

how to return meteor call value in helper in meteor

I have pass id to helper from html and my helper is getusername

getusername(id) {
var username = "";
Meteor.call("getUserName", id, function(error, result) {
if (error) {
console.log("error", error);
}
if (result) {
console.log(result);
username = result;
}
});
return username;
}


while i log the result its log what i need but in UI no username visible.
and when i use this with Reactive var its become infinity ... because when reactive variable value change its execute again...

Answer

because of async behavior you wont get the return value what you can do is set the return value in session and get it from session where ever you want

getusername(id) {
    var username = "";
    Meteor.call("getUserName", id, function(error, result) {
        if (error) {
            console.log("error", error);
        }
        if (result) {
            console.log(result);
            username = result;
        }
    });
    Session.set('getUserNameResult', result);
}