Synchronous call from Client to Server Method Call

Hello,

Working on a meteor project and totally blocked on the below issue

  • Customers Adds item to cart

  • On clicking the “Place Order” capturing the ‘click’ event

  • Calling a method on server sample code below Meteor.call()

  • Now on the server side the method invokes some external API

  • So I want to wait for the response back in the client examine the result and route to the confirmation page or display error message.

  • With the call back, the control the transferred to default/current page and redirecting the the confirmation page and I have no way to examine the transaction is success of not

                     Meteor.call(‘placeOder', order info, sequence, function(error, result)
                     {
    
                         if(error)
                         {
                             if(result)
                             {
                                 console.log("Could not insert the order for the session  = " + sessid + "Order = " + JSON.stringify(result, null, 4));
                             }
                             else
                             {
                                 console.log("Could not insert the order for the session  = " + sessid );
                             }
    
                         }
                         else
                         {
    
                             Session.set('orderStatus', 'Success');
                             Router.go('confirmation',  {UniqueId: result.id});
    
                         }
    
                     });
    

Any help to resolve would be greatly appreciated. Basically on client side I want to wait for response back from server, examine the response and route accordingly.

Jay

Any idea of suggestion folks?

Have you tried wrapping the external API call in a Meteor.wrapAsync function?

1 Like

Maybe there is an answer here?

1 Like

hey @jayjo7,

the missing, important piece in your post is the server code

please show it so we can figure what (and how) you return from the server

What the guys above me said! Your problem is (very very likely) about the call to an external API being async and having to use wrapAsync (or fibers/future) to properly deal with that and return something meaningful to the client.

Also, if you like, here are some suggestions to improve the code you posted:

Meteor.call(‘placeOder', order info, sequence, function(error, result)
            {

                if(error)
                {
                    // in Meteor-land and in nodejs-land you will never get *both* error AND result back
                    // so remove this whole next check. If error is set, then it's an error and no result.
                    //console.log("Could not insert the order for the session  = " + sessid ); // is all that makes sense here
                    if(result)
                    {
                        console.log("Could not insert the order for the session  = " + sessid + "Order = " + JSON.stringify(result, null, 4));
                    }
                    else
                    {
                        console.log("Could not insert the order for the session  = " + sessid );
                    }

// state return; here, then your code becomes clearer, like this:
return;
// and now there's no need for an else {} block because you will have already returned from the function in case of error, and can just keep going if there was no error
                }
                else
                {

                    Session.set('orderStatus', 'Success');
                    Router.go('confirmation',  {UniqueId: result.id});

                }

            });

Put together the idiom for handling errors in callbacks looks like this:

Meteor.call('placeOder', orderInfo, sequence, function (error, result) {
  if (error) {
    console.log("Could not insert the order for the session  = " + sessid); // sessid undefined, by the way
    return;
  }
  Session.set('orderStatus', 'Success');
  Router.go('confirmation', {UniqueId: result.id});
});

In plain words: “if error set, handle error and return; (otherwise) proceed as normal.” This helps keep levels of indentation at sane and manageable levels.