Reputation: 2729
I've got a button on my app dashboard that says "access e-learning portal".
Clicking that button calls a Meteor.method which queries the e-learning portal API to receive an access link (single sign on).
I've got this working fine.
What I can't work out is how to then send that link from the server to the client so I can redirect them to it. I just cannot wrap my head around synchronous, asynchronous, callbacks etc. I do not understand why the method doesn't just send the client its result. My result is undefined
SO from the users point of view: click button -> e-learning portal opens in new tab (or same tab whatever)
from apps point of view: click button -> template.event -> meteor.call ->run server method -> retrieve URL (I've successfully got this far) -> send user to retrieved url (e.g) http://www.google.com
Here's my code:
Event:
Template.dashboard.events({
'click #lmsAccess': function(e, t) {
e.preventDefault();
Meteor.call('lmsLink', function(error, result) {
if(error) {
console.log(error.reason);
return;
}
console.log(result); //returns as undefined
});
}
});
Method
if (Meteor.isServer) {
Meteor.methods({
'lmsLink': function () {
//removed all my irrelevant variables from here for stack question
HTTP.call( verb, 'http://sandbox.wceaapi.org/'+ endPoint, {
headers: {
"Request-Time": timeStamp,
"Api-Key": key,
"Signature": hash
}
}, function( error, response ) {
if ( error ) {
console.log( error );
} else {
console.log( response );
var result = JSON.parse(response.content);
var accessLink = result.records.accessLink;
console.log(accessLink);
return accessLink;
}
});
}// end lmsLink
});//end method
}
Upvotes: 1
Views: 589
Reputation: 141
If you have the url client side (via the method callback function) you can simply use
location.href = returnedUrlFromMethod;
This will redirect the user to that url.
Upvotes: 4