Ich versuche, Json-Daten von einer API mit Meteor-Methode zu bekommen, ich habe versucht, Meteor WrapAsync sowie Node Future zu verwenden. Unten ist mein Code:Meteor wrapsAsync/Node Fiber Future Funktioniert nicht
Template Helper - Client Side
getLocationTimebyAPI: function (company_location) {
Meteor.call('getLocationTimebyAPIServerMethod', company_location, function(error, results){
if(error){
console.log('error',error.reason);
} else {
var localtime = results.data.data.time_zone[0].localtime
var utcoffset = results.data.data.time_zone[0].utcOffset
console.log(localtime+ ' '+utcoffset);
var returntext = localtime+' (UTC '+utcoffset+')';
return returntext;
}
});
}
Methode 1: Verwenden Meteor wrapAsync - Server Side
'getLocationTimebyAPIServerMethod': function(company_location){
var apiurl = 'http://api.worldweatheronline.com/free/v2/tz.ashx?q='+company_location+'&format=json&key=XXXXXX';
var convertAsyncToSync = Meteor.wrapAsync(HTTP.get),
resultOfAsyncToSync = convertAsyncToSync(apiurl);
return resultOfAsyncToSync;
}
Methode 2: Verwenden Knoten Fiber Future- Serverseite
'getLocationTimebyAPIServerMethod': function(company_location){
// use the node fibers npm
var Future = Npm.require('fibers/future');
var apiurl = 'http://api.worldweatheronline.com/free/v2/tz.ashx?q='+company_location+'&format=json&key=XXXXXXXX';
// Create our future instance.
var future = new Future();
HTTP.get(apiurl, {}, function(error, response) {
if (error) {
future.return(error);
} else {
future.return(response);
}
});
return future.wait();
}
In beiden Methoden bekomme ich die Werte in der Konsole gedruckt, aber sie werden nicht zurückgegeben.
Unten finden Sie den Screenshot:
Ich weiß nicht, wo ich falsch bin, Könnte jemand mir bitte etwas vorschlagen.
Edited: Added Template Code:
<tr>
<td>Local Time:</td>
<td><input id="company_location_time" name="company_location_time" type="text" size="23" placeholder="Lead Company Location Time" value="{{getLocationTimebyAPI company_location}}" readonly style="background:#7FAAFF;font-weight:bold;"><p style="font-size:8px;">Local Time Powered By <a style="font-size:8px;" href="http://www.worldweatheronline.com/search-weather.aspx?q={{company_location}}" target="_blank">World Weather Online</a></p></td>
</tr>
Wo und wie rufst du '' getLocationTimebyAPI?Bitte geben Sie ein Beispiel an –
Oben ist der Vorlagencode, der die Template-Hilfsfunktion aufruft – Manu