简体   繁体   中英

node.js node-soap - How to set a client instance as a property of a parent object?

I am trying to build a module to handle SOAP functionality for the sake of modularity

I use vpulim/node-soap , allthough it's not relevant for my question:

var soap = require ( "soap" );

    function SoapController( url ) {

        var self = this; //prevent scope issues

            self.GetClient = function () {

                soap.createClient(
                    url , function ( err , client ) {

                                   self.client = client;
                      console.log( self.client.describe() );
            }
        )
    };
}

module.exports = SoapController;

In my Router:

SoapOb = require ( "./SoapController.js" );

router.get(
    '/S' , function ( req , res ) {

        var soapC = new SoapOb ( 'http://infovalutar.ro/curs.asmx?wsdl' );

        soapC.GetClient();

        console.log( soapC );
    }
);

Results of conlog: from inside the router:

SoapController { GetClient: [Function] }

And inside the callback of the createClient method:

{ Curs:
   { CursSoap:
      { GetLatestValue: [Object],
        getlatestvalue: [Object],
        getall: [Object],
        getvalue: [Object],
        getvalueadv: [Object],
        GetValue: [Object],
        LastDateInserted: [Object],
        lastdateinserted: [Object] },
     CursSoap12:
      { GetLatestValue: [Object],
        getlatestvalue: [Object],
        getall: [Object],
        getvalue: [Object],
        getvalueadv: [Object],
        GetValue: [Object],
        LastDateInserted: [Object],
        lastdateinserted: [Object] } } }

What I need to acomplish is set the client Instance as a property of the SoapController object so i can access it's methods .

I also tried defining the GetClient method through prototype,but it doesn't work , I get undefined in console

SoapController.prototype.GetClient = function () {

    var self = this; //prevent scope issues

        soap.createClient(
            self.url , function ( err , client ) {

            self.client = client;
        }
    )
};

Please guide me !!!

soap.createClient is an async method. So you have to refactor your getClient method.

SoapController.prototype.getClient = function (callback) {
    var self = this;

    soap.createClient(self.url, function (err, client) {
        self.client = client;
        callback();
    })
}

now any work you need done must be done inside callback.

SoapOb = require ( "./SoapController.js" );

router.get('/S' , function ( req , res ) {
    var soapC = new SoapOb ( 'http://infovalutar.ro/curs.asmx?wsdl' );
    soapC.GetClient(function() {
        console.log(soapC.client);
    });
}

);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM