I am trying to call a simple weather service synchronously in meteor. I am not even able to make the client creating synchronous. In the code below I expect user.testData to contain "test data" but it contains nothing.
if (Meteor.isClient) {
Template.confirmWeather.onRendered(function(){
var validator = $('.confirmWeatherAndGoToMessage').validate({
submitHandler: function(event){
Meteor.bindEnvironment(Meteor.call('testWeather',Meteor.bindEnvironment(function(error,result)
{
if (error)
{
console.log(error.message)
}
else
{
var userId = Meteor.userId();
var user = Meteor.users.findOne({_id: userId});
if (user.testData)
{
console.log(user.testData);
}
}
})));
}
});
});
}
if (Meteor.isServer) {
Meteor.methods({
testWeather: function () {
var soap = Npm.require('soap');
var options = {
ignoredNamespaces: {
namespaces: [],
override: true
}
}
var url = 'http://www.webservicex.com/globalweather.asmx?WSDL';
Meteor.wrapAsync(soap.createClient(url, options,Meteor.bindEnvironment(function(err, client) {
if (err)
{
console.log("CREATE ERROR:");
console.log(err);
}
else
{
Meteor.wrapAsync(Meteor.call("insertIntoTestData","test data",function(err,res)
{
if (err)
{
throw new Meteor.Error("server-error",error.message);
}
else
{
console.log('DATA: ');
}
}));
}
})));
},
insertIntoTestData: function(data) {
var userId = Meteor.userId();
var user = Meteor.users.findOne({_id: userId});
if (user)
{
return resultId = Meteor.users.update({_id:userId},{$set:{testData:data}});
}
}
});
}
If you can't make that approach work maybe i can offer alternative. When i needed sync calls with request library i used different approach by using future library from fibers/future nodejs. After the request i just put "future.wait()" and inside async code callback "future.return(value)". Execution would then wait until request was finished and returned a value. This link here nicely explains use of that approach also: https://themeteorchef.com/tutorials/synchronous-methods
Best regards,
Dino