node.js node-soap - 如何将客户端实例设置为父对象的 属性?

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

为了模块化,我正在尝试构建一个模块来处理 SOAP 功能

我使用 vpulim/node-soap ,尽管它与我的问题无关:

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;

在我的路由器中:

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 );
    }
);

conlog 的结果: 从路由器内部:

SoapController { GetClient: [Function] }

并且在 createClient 方法的回调中:

{ 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] } } }

我需要完成的是将客户端实例设置为 SoapController 对象的 属性,以便我可以访问它的方法。

我也尝试通过原型定义 GetClient 方法,但它不起作用,我在控制台中得到 undefined

SoapController.prototype.GetClient = function () {

    var self = this; //prevent scope issues

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

            self.client = client;
        }
    )
};

请指导我!!!

soap.createClient 是一种异步方法。所以你必须重构你的 getClient 方法。

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

    soap.createClient(self.url, function (err, client) {
        self.client = client;
        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);
    });
}

);