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

标签 javascript node.js soap constructor prototype-chain

为了模块化,我正在尝试构建一个模块来处理 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 对象的属性,以便我可以访问它的方法。

我也尝试通过原型(prototype)定义 GetClient 方法,但它不起作用,我在控制台中未定义

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

);

关于javascript - node.js node-soap - 如何将客户端实例设置为父对象的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35581810/

相关文章:

javascript - 单击时,将类切换到父级之外

windows - Node.js 无法在 Windows 上安装全局 npm 包(不是路径)

soap - WSDL 中的 xmlns 不正确

php - 在/不发送请求之前检查由 PHP SoapClient 调用创建的 XML

java - 组播错误: Multicast Address Not Found

javascript - 黑客攻击/覆盖 Javascript

javascript - 无法配置react-router在plunker上运行

javascript - 从回调中获取值

javascript - LocalStorage 在警报 block 期间未更新

javascript - 当我在生产模式下将源代码与 webpack 捆绑时,如何避免丢失类名?