The result of returning Meteor.http in the method

I have a Meteor method that goes around http.get. I am trying to return the results from this http.get to the returned method in order to use the results when calling the method.
I can not make it work.

Here is my code:

(In the shared folder)

Meteor.methods({ getWeather: function(zip) { console.log('getting weather'); var credentials = { client_id: "string", client_secret: "otherstring" } var zipcode = zip; var weatherUrl = "http://api.aerisapi.com/places/postalcodes/" + zipcode + "?client_id=" + credentials.client_id + "&client_secret=" + credentials.client_secret; weather = Meteor.http.get(weatherUrl, function (error, result) { if(error) { console.log('http get FAILED!'); } else { console.log('http get SUCCES'); if (result.statusCode === 200) { console.log('Status code = 200!'); console.log(result.content); return result.content; } } }); return weather; } }); 

For some reason, this does not return results , even if they exist, and the http call works : console.log (result.content); really logs the results.

(client folder)

  Meteor.call('getWeather', somezipcode, function(error, results) { if (error) return alert(error.reason); Session.set('weatherResults', results); }); 

Of course, here the session variable ends up empty.
(Note that this part of the code seems fine, as it returned appropriately if I hardcoded the return with some dummy string in the method.)

reference

+5
source share
3 answers

In your example, Meteor.http.get runs asynchronously.

See documents :

HTTP.call (method, url [, options] [, asyncCallback])

On the server, this function can be performed either synchronously or asynchronously. If the callback is omitted, it is executed synchronously and the results are returned after the query completes successfully. If the request was not successful, the error was reset

Switch to synchronous mode by removing asyncCallback:

 try { var result = HTTP.get( weatherUrl ); var weather = result.content; } catch(e) { console.log( "Cannot get weather data...", e ); } 
+14
source

Cuba Virobek is right, but you can also call HTTP.get asynchronously and use the future to stop the method until get answers:

 var Future = Npm.require('fibers/future'); Meteor.methods({ getWeather: function(zip) { console.log('getting weather'); var weather = new Future(); var credentials = { client_id: "string", client_secret: "otherstring" } var zipcode = zip; var weatherUrl = "http://api.aerisapi.com/places/postalcodes/" + zipcode + "?client_id=" + credentials.client_id + "&client_secret=" + credentials.client_secret; HTTP.get(weatherUrl, function (error, result) { if(error) { console.log('http get FAILED!'); weather.throw(error); } else { console.log('http get SUCCES'); if (result.statusCode === 200) { console.log('Status code = 200!'); console.log(result.content); weather.return(result); } } }); weather.wait(); } }); 

In this case, there is no particular advantage to this method with respect to synchronous get , but if you ever do something on a server that can benefit from something like an HTTP call made asynchronously (and thus without blocking the rest of the code in your method), but you still need to wait until this call returns before the method can, then this is the right solution. One example is the execution of several non-contingent get s, which all would have to wait for each other if they were to be executed synchronously.

More details here .

+2
source

Asynchronous calls are sometimes preferred. You can use the async / await syntax for this, and you need to promise HTTP.get .

 import { Meteor } from 'meteor/meteor'; import { HTTP } from 'meteor/http'; const httpGetAsync = (url, options) => new Promise((resolve, reject) => { HTTP.get(url, options, (err, result) => { if (err) { reject(err); } else { resolve(result); } }); }); Meteor.methods({ async 'test'({ url, options }) { try { const response = await httpGetAsync(url, options); return response; } catch (ex) { throw new Meteor.Error('some-error', 'An error has happened'); } }, }); 

Note that the meteor test method is marked async . This allows you to use the await operator inside it with method calls that return Promise . The lines of code following the await statements will not be executed until the promised promise is found. If the promise is rejected, a catch block will be executed.

0
source

Source: https://habr.com/ru/post/1201851/


All Articles