gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 07:36:43 24 4
gpt4 key购买 nike

为了模块化,我正在尝试构建一个模块来处理 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com