sdk-node icon indicating copy to clipboard operation
sdk-node copied to clipboard

Why the execute function on APIOperationBase class does not return the response?

Open comeacross1989 opened this issue 5 years ago • 3 comments

Hi, I'm using a firebase callable function and would like to send the repsponse back to the client once the transaction is completed - instead of sending another post to a different URL. The only problem is that the APIOperationBase.execute does not return the response. Is there a reason for this?

It would be much more efficient if I could do this... ` try {

    const THE_RESPONSE_FOR_CLIENT = ctrl.execute(function(){

        var apiResponse = ctrl.getResponse();
        var response = new ApiContracts.CreateTransactionResponse(apiResponse);

        //pretty print response
        console.log(JSON.stringify(response, null, 2));

        if(response != null){
            if(response.getMessages().getResultCode() == ApiContracts.MessageTypeEnum.OK){
                if(response.getTransactionResponse().getMessages() != null){
                    console.log('Successfully created transaction with Transaction ID: ' + response.getTransactionResponse().getTransId());
                    console.log('Response Code: ' + response.getTransactionResponse().getResponseCode());
                    console.log('Message Code: ' + response.getTransactionResponse().getMessages().getMessage()[0].getCode());
                    console.log('Description: ' + response.getTransactionResponse().getMessages().getMessage()[0].getDescription());
                    
                   
                }
                else {
                    console.log('Failed Transaction.');
                    if(response.getTransactionResponse().getErrors() != null){
                        console.log('Error Code: ' + response.getTransactionResponse().getErrors().getError()[0].getErrorCode());
                        console.log('Error message: ' + response.getTransactionResponse().getErrors().getError()[0].getErrorText());
                        
                      }
                }
            }
            else {
                console.log('Failed Transaction. ');
                if(response.getTransactionResponse() != null && response.getTransactionResponse().getErrors() != null){
                
                    console.log('Error Code: ' + response.getTransactionResponse().getErrors().getError()[0].getErrorCode());
                    console.log('Error message: ' + response.getTransactionResponse().getErrors().getError()[0].getErrorText());
                    
                  }
                else {
                    console.log('Error Code: ' + response.getMessages().getMessage()[0].getCode());
                    console.log('Error message: ' + response.getMessages().getMessage()[0].getText());
                   
                  }
            }
        }
        else {
            console.log('Null Response.');  
        }
        callback(response,finalMessage);
        console.log('response,finalMessage');
        console.log(ctrl._response );
    });


    return THE_RESPONSE_FOR_CLIENT;
} catch (error) {
    throw new functions.https.HttpsError('unknown', error);
}`

comeacross1989 avatar Nov 01 '19 19:11 comeacross1989

I guess a better question would be, why is it an async callback function? I want to send the response back to the user!

comeacross1989 avatar Nov 01 '19 21:11 comeacross1989

Look at this stack overflow question. By definition all callback functions are async so you would have to force your code to wait for the promise to resolve to send it back to the user.

The problem is, if you're using an environment like gcp or firebase, you won't be able to change the auothorizenet npm package in the cloud to force the await for promise to resolve. Not sure what to tell you there. Fork off of this package? I'm curious what the contributors think. @sinayak @maria-simon @lilithmm

Marcusg62 avatar Nov 02 '19 16:11 Marcusg62

Your code attempt would cause a LOT of issues because you are assuming not one but two network connections are going to remain stable during the entire transaction. There is a reason why it's built async and with promises. Also your code looks like java instead of taking advantage of Promises ... You really need to rethink your design.

ahmed-masud avatar Apr 16 '20 16:04 ahmed-masud