How to create client side asynchronous function for meteor react?

Please help me for this case. I try to import or pass data from asynchronous function to client side.
Event i use session on server the client still cannot get value from that session also.
Meteor.methods({
searchResult: function() {
if (Meteor.isServer) {
this.unblock();
var result = HTTP.call(‘GET’, ‘http://localhost:9200/store/product/_search’);
Session.set(‘resultData’, result.statusCode);
}
}
});

And console.log(Session.get(‘resultData’)
the result of console.log still undefined

Any idea on this case please help me. Thanks advanced

If you put your methods in a file under a server/ folder you don’t need to use if (Meteor.isServer). You should probably also avoid this.unblock() - it won’t necessarily provide any benefit here.

This is how it could be done.

In server/methods.js:

Meteor.methods({
  searchResult() {
    var result = HTTP.call('GET', 'http://localhost:9200/store/product/_search');
    return result;
  }
});

Then, in your client code (in a file under a client/ folder):

Meteor.call('searchResult', (error, result) => {
  if (error) {
    console.log('something broke');
  } else {
    console.log(result); // result is available here
  }
});

If you want to use the result outside of that callback you will likely need to use a reactive variable.

Hello, Robfallows,
I ready change create file name: methods.js inside folder server, unfortunately i cannot call the method that i just created.
The error: cannot find method name "searchResult"
meteor version 1.4

We need to see your code, can you share it?

Hi Robfallows,

Now i try to update follow you guide the problem not found don’t happen. But i still cannot pass the result out of asynchronous function.

The result of console is undefined both.

This is correct, the result will only be available once it comes back asynchronously.

Meaning the two console.log statement will be undefined.

Once the result comes back you change the mydata state and you component will rerender (depending on what you do with the state)

How i can pass the result out of asynchronous function ?

Check out the Meteor Guide on using Meteor’s data system with React.