How to properly pass Event Parameters to SOAP Option in Node.js? The SOAP call in my code is executed before the Option variable is ready

29
May 15, 2019, at 10:30 PM

I am new to Node.js

I am trying to create a REST API on API Gateway of AWS by consuming an external public SOAP Web Service. (http://webservices.oorsprong.org/websamples.countryinfo/CountryInfoService.wso?WSDL)

I am not able to make it work when I try to use the user input in Event Parameter as the SOAP argument. But if I hard code the SOAP argument, it works.

I guess the problem is that when I use user input in Event Parameter as the SOAP argument, the SOAP Service was called before the argument is ready, because Node.js is asynchronous.

Any thought? Thank you.

var soap = require('soap');
var url = process.env.ENDPOINT_URL;
exports.handler = function (event, context, callback) {
    var input = '';
    soap.createClient(url, function (err, client) {
        if (event.Country && event.Country!=="") {
            input = {sCountryISOCode: event.Country};
            // input = {sCountryISOCode: 'US'};
            client.FullCountryInfo(input, function (err, result) {
                var fullJsonFormatRes = {
                    "statusCode": 200,
                    "headers": {
                    "my_header": "my_value"
                    },
                    "body": JSON.stringify(result),
                    "isBase64Encoded": false
                };
                callback(null, fullJsonFormatRes);
                // });
            });
        };
    });
};

Expected Result (if I use var input = {sCountryISOCode: 'US'};)

Response:
{
  "statusCode": 200,
  "headers": {
    "my_header": "my_value"
  },
  "body": "{\"FullCountryInfoResult\":{\"sISOCode\":\"US\",\"sName\":\"United States\",\"sCapitalCity\":\"Washington\",\"sPhoneCode\":\"1\",\"sContinentCode\":\"AM\",\"sCurrencyISOCode\":\"USD\",\"sCountryFlag\":\"http://www.oorsprong.org/WebSamples.CountryInfo/Images/USA.jpg\",\"Languages\":{\"tLanguage\":[{\"sISOCode\":\"eng\",\"sName\":\"English\"}]}}}",
  "isBase64Encoded": false
}

Current failed result (if I use input = {sCountryISOCode: event.Country};)

Response:
{
  "statusCode": 200,
  "headers": {
    "my_header": "my_value"
  },
  "body": "{\"FullCountryInfoResult\":{\"sISOCode\":\"\",\"sName\":\"Country not found in the database\",\"sCapitalCity\":\"\",\"sPhoneCode\":\"\",\"sContinentCode\":\"\",\"sCurrencyISOCode\":\"\",\"sCountryFlag\":\"\",\"Languages\":null}}",
  "isBase64Encoded": false
}
READ ALSO
Keycloak Policy Enforcing for nodejs REST Api

Keycloak Policy Enforcing for nodejs REST Api

It's the first time asking something here, so excuse if I forget to provide some information or any other mistakesSorry for the text wall

48
Sum of n number inside array is x number and create subset of result

Sum of n number inside array is x number and create subset of result

For example, if you have an array [20, 6, 7, 8, 50] and if I pass value 21 it should return [6,7,8] this sub arrayNote: the sum of number should be in sequence

13
Initializing multiple dbs with async await at the top level in node

Initializing multiple dbs with async await at the top level in node

I understand from a couple of similar questions that to use async await at the top level, it is necessary to wrap up anonymous functions

9
Connection refused between Node and MongoDB

Connection refused between Node and MongoDB

Getting this error when I try to connect my Node to my MongoDB

21