0

I am toying around the some of the tutorials, the todo list is structured as follows: The server main.js imports a file, tasks.js, which describes some operations (some of which are in a "IsServer" statement") The client main.jsx imports App.jsx which imports the same tasks.js file

I wanted to add a call to an external service, so I've extended the server main.js (the one inside the /server folder) with a method like this:

Meteor.methods({
    calloutMethod: function(id){
        var apiUrl = 'http://someurl/'+id;      
        return HTTP.call(
        "GET", 
        apiUrl
        );

    },...

now in the tasks.js, this file is somewhere inside the import folder and is imported also from the client, I've added:

Meteor.methods({
    'tasks.insert'(id) {
    ...


    Meteor.call('calloutMethod', id, function(error, result){
    if(!error){
        console.log(result);
    }

});

I would like to call the method tasks.insert from the client, this method should call the calloutMethod hosted on the server.

When I put a console.log inside the server method I see the call happening and the result are displayed on the server console.. in the client console I immediately see "undefined" meaning that the callback function is immediately called:(

Is it because of this hybrid "import folder" making confusion? In my understanding of the documentation using Metor.call('name',params, function(err,res){... should make an asynchronous call to the server method, why is it not happening?

T. Rossi
  • 465
  • 1
  • 6
  • 23
  • The problem is that you have an asynchronous call inside the method. Nothing to do with imports. See http://stackoverflow.com/questions/12569712/meteor-calling-an-asynchronous-function-inside-a-meteor-method-and-returning-th – Michel Floyd Mar 23 '17 at 01:02
  • Possible duplicate of [Meteor: Calling an asynchronous function inside a Meteor.method and returning the result](http://stackoverflow.com/questions/12569712/meteor-calling-an-asynchronous-function-inside-a-meteor-method-and-returning-th) – Michel Floyd Mar 23 '17 at 01:02
  • I've read those entries, they explain how receiving the response from the external service to meteor server, which it's working, what I can't make it work is to have meteor client wait for the server, I am not sure if I explain it right:/ – T. Rossi Mar 23 '17 at 01:17
  • I think I've got the problem clearer, the client passes the callback which is executed on the server when the data arrives... I'd like to have the server to pass response and error to the client and to have the execution happen on the client – T. Rossi Mar 23 '17 at 01:29
  • Yep - this is probably the #1 question for Meteor methods: how to return data from an async function. [Meteor.wrapAsync()](https://docs.meteor.com/api/core.html#Meteor-wrapAsync) is probably your simplest solution. – Michel Floyd Mar 23 '17 at 02:11

0 Answers0