Synchronous Method Calls on the Server

I have a Meteor Method (method1) that calls another Meteor Method (method2), both executing on the server. The second method (method2), makes some API calls, saves the results in the db. Method1 synchronously calls method2, and then retrieves the saved db results to do some other actions.

Method2 is basically a sync function for an external data source to run before executing the remaining code in method1.

The problem I am running into is that method1 is not able to view/access the updated db data from method2. My understanding of Asynchronous/Synchronous operation between the client/server in Meteor is not very good, and I think I have an error with how my methods are being called.

Does anyone know if there is an obvious error below? I am using meteorhacks:npm with the Async.runSync method to do the API calls.

My code is very long, but here is some pseudo code of what I am doing:

Meteor.methods({
  method1: function(id) {
    check(id, String);
    if (Meteor.isServer) {
      var bid = Bids.findOne({_id: id});
      
      var dataSync = Meteor.call('method2', bid._id); // returns true if no errors, should pause execution until all db updates are complete

      var bid_item = Bids.findOne(_id: id}).api_data;  // Data saved from method2

      // do some stuff with bid_item....
    }
  },
  method2: function(id) {
    check(id, String);

    if (Meteor.isServer) {
      var bid = Bids.findOne({_id: id});
      
      var apiCallData = Async.runSync(
        // api call....
      )

      var update = Bids.update(id, {$set {data: apiCallData}});  // synchronous update call on server
    }
    return true;
  }
});

What’s happening when you try to access the updated data in method1; are you getting the wrong data, getting nothing, getting an error/exception, etc.?

Also, I’m assuming that your real code has the missing ‘{’ before _id in:

var bid_item = Bids.findOne(_id: id}).api_data;  // Data saved from method2

The updated data in method1 just returns as undefined. I can access the object/document, but the field I need is not defined. The second time I run method1, the data can then be accessed. I am assuming this is from the first call of method2; given it is a sync method, it’s fine if the second call is immediately after the first call, but this isn’t always the case.

It’s worth noting that I tried running a while loop and a Meteor.setTimeout method on the data call to the updated data to get method1 to pause execution until the data can be found, but I didn’t have any luck with this. It just got stuck in an infinite loop.

Thanks for catching that typo too…yes in the original code it’s working :slight_smile:

Hard to see from the code, but method1 does not return anything, it is normal that you get undefined from it.
Second point is that there is rarely any need to do Meteor.call() on the client side, soyou can put them in the /server dir and skip the isServer. Third: You do not have to call Meteor.call() from a Meteor.call(), you can define a “normal” method inside the server/mymethods,js
var method2: function(){
return something
}
Meteor.methods({
method1: function(){
var m2 = method2(arg)
}
})

Might be easier to control the flow

Also, if you do a lot of db stuff, it might be easier/safer to just do them in one method and let their callbacks dictate flow

Bids.update(qry, function(e,r){
if(e) //throw err
return r
})

or something…