0

I am working with soap in Node typescript.

I want to return a boolean after performing a soap request to the server in the strong-soap package.

The code looks like:

public isSupplierBalance(debtorAccount: number): boolean {
        let economicToken = this.token;

    soap.createClient(this.WSDL, {}, function (err, client) {
        let method = client['ConnectWithToken'];
        let args = {
            token: economicToken,
            appToken: economicAPI.EconomicN1Key
        };
        method(args, function (err, result, envelope, soapHeader) {
            if (err) {
                console.log(err);
            }
            let session = cookieParser(client.lastResponseHeaders);
            let args = {
                creditorHandle: {
                    Number: debtorAccount
                }
            };
            client.addHttpHeader('Cookie', session);
            method = client['Creditor_GetOpenEntries'];
            method(args, function (err, result, envelope, soapHeader) {
                if (err) {
                    console.log(err);
                }
                console.log(result.toString());
                return (typeof result.Creditor_GetOpenEntriesResult === 'undefined');
            });
        });
    });
}

Basically I want to return if the result.Creditor_GetOpenEntriesResult contains any values of not. I have seen in the documentation of strong-soap that an async approach can be used, and I guess that should be my approach but any test towards that solution have just failed. https://www.npmjs.com/package/strong-soap

Patrick Vibild
  • 405
  • 1
  • 3
  • 16

1 Answers1

1

Hope this helps. I was able to wrap it with a new Promise

async function callSOAP(options: {}, methodname: any, methodparams: any) {
let finalresult: any;

    return new Promise((resolve, reject) => {
        soap.createClient(url, options, async function (err: any, client: any) {
            const { result, envelope, soapHeader } = await client[methodname](methodparams);
            finalresult = { result, envelope, soapHeader }
            // return { result, envelope, soapHeader };
            if (err) {
                reject(err);
            }
            else { resolve(finalresult) };
        });
    }

The calling code would look like this

 result = await callSOAP(options, methodname, methodparams).then((r1) => r1);
tmez
  • 156
  • 1
  • 7