Search code examples
javascriptexpresssoapsoap-client

SOAP with ExpressJS


Basically I'm trying to setup a SOAP API together with my ExpressJS. This is a project for my new job after taking it by the others that left the company and left a maze out there.

They have over 50 laravel servers for some reasons for their apps and use the same things over and over again which is confusing for me at the moment.

Doing this kind of stuff with SOAP in PHP is way easier than trying to move the entire stuff under my Express Server. What I'm planning for the future is to make 1 server that handles everything than having hundreds of API calls from x folder in those other hundreds of folders.

Regardless, I'm here to as for your help...

So, I've connected to the SOAP server that they use for logistics inside here, they require sessionID which I extract from auth in a .txt file as there is no way known for me to extract it from client.method.

Eitherway, somehow I hardcoded my headers with.. Headers Model I think

client.addSoapHeader(nameServer, 'UserSessionCredentials', options);
client.soapHeaders[0].options = options; //Options are always empty even passing the object in the .addSoapHeader That's why hardcoding it this way...maybe I'm wrong

in XML / WSDL is something like this

<UserSessionCredentials xmlns="http://www.website.net"> (changed the name for more privacy)
<UserId>int</UserId>
<clientId>string</clientId>
<SessionId>string</SessionId>
</UserSessionCredentials>

Not sure if that is even correct as I couldn't figure the next step, which, by nature is calling the method inside to extract my data from their platform...

It's something on the nature GetData() which require a template which I made and should work properly, or even so, I should get a error regardless with something missing from headers or something...

The method is this, at least the basics of it cause I need to know what I extract, even if it's an empty object is still good for me as I will have a point to go.

            client.GetData(obj, function (err, result) {
                if (err) {
                    console.log(err);
                }
                console.log(result);
            });

The error bellow it's happening at client.GetData / client.GetReportData or any function that they have.

at D:\Projects\WMS\WMSIntegrations\Server\lib\auth\pvxData.js:69:20

Been trying various methods to be able to make it work in Express but I get the same errors.

D:\Projects\WMS\WMSIntegrations\Server\node_modules\strong-soap\src\parser\xmlHandler.js:473
    parser.onerror = function(e) {
                   ^

TypeError: Cannot set properties of undefined (setting 'onerror')
    at XMLHandler.parseXml (D:\Projects\WMS\WMSIntegrations\Server\node_modules\strong-soap\src\parser\xmlHandler.js:473:20)
    at Client.addSoapHeadersToEnvelope (D:\Projects\WMS\WMSIntegrations\Server\node_modules\strong-soap\src\base.js:134:20)
    at Client._invoke (D:\Projects\WMS\WMSIntegrations\Server\node_modules\strong-soap\src\client.js:212:10)
    at Client.GetData (D:\Projects\WMS\WMSIntegrations\Server\node_modules\strong-soap\src\client.js:103:12)
    at D:\Projects\WMS\WMSIntegrations\Server\lib\auth\pvxData.js:69:20
    at D:\Projects\WMS\WMSIntegrations\Server\node_modules\strong-soap\src\soap.js:54:5
    at D:\Projects\WMS\WMSIntegrations\Server\node_modules\strong-soap\src\soap.js:40:7
    at D:\Projects\WMS\WMSIntegrations\Server\node_modules\strong-soap\src\parser\wsdl.js:45:7
    at D:\Projects\WMS\WMSIntegrations\Server\node_modules\strong-soap\src\parser\wsdl.js:130:9
    at WSDL._processNextInclude (D:\Projects\WMS\WMSIntegrations\Server\node_modules\strong-soap\src\parser\wsdl.js:196:14)

Basically that abstract name "obj" is the template that the report requires to go, but, I still can't figure why the error is happening... Maybe any of you got an idea with SOAP as I don't. I know it's a learning process, but, as previously stated, I can make this work with Python or PHP, but I'm not familiar with them and I would want to make all my routes and calls in Express as I'm not a very experienced developer.

My role is called "System Admin" and my attributes aren't related to developing, but I will take the opportunity to develop my analytical and coding skills. (PS. There are no paid courses by company for me to develop the required skills, what I do now is work offline as well to figure a way how to make things work in Express)

In the worst case scenario, I will have to learn PHP(more than basics that I know now) and Laravel(which I have no idea how to work with). I will be honest, I don't like how the PHP is written, and, Laravel is even more confusing.

I appreciate all of you who took the time to read my stuff.


Solution

  • thank you for taking your time to read all the post. After more brainstorm, I decided to take them 1 by one to see where I was doing the mistake as everything was logical in the code and my head. Turned out to be the headers that were incorrectly written. I was passing to many variables in the headers as I saw what other colleagues were writing in their PHP code. They were passing the nameserver and "UserSessionCredentials" into theirs and I thought I have to pass them as well as in PHP works as they did, turns out for Javascript is easier. The command is like this and I will just do my object but emptied.

    const headerObj = {
        clientID: process.env.CLIENT_ID, //clientID is static
        userID: process.env.USERNAME_ID, //UserID is always the same, special made for integrations
        sessionID: MySessionID,//from a file, I take it from authenticate method
    }
    
    client.addSoapHeader(headerObj);
    

    All I had to do is to remove everything and pass only my object with clientid, userid and sessionid for the user and magically it works for me.

    Hopefully in the following days, weeks, months or years if any of you hit with the same difficulties as I did, will help you.