【发布时间】:2016-09-04 18:35:48
【问题描述】:
我在尝试在异步soap请求中调用对象函数时遇到了非常非常困难的事情。基本上可以归结为:
function Thing(request, response) {
this.foo = function() {
console.log("this is foo");
}
this.doThing = function() {
// Get SOAP args
args = { foo: this.request.cookies.foo };
// From the SOAP npm package library
soap.createClient(function(err, client) {
client.doSomething(args, function(err, result) {
// Somehow call foo(); <--- CAN'T FIND A WAY TO DO THIS
});
});
}
}
// Make it so I can access this.request and this.response somehow
Thing.prototype = Object.create(AbstractThing);
我已经尝试了很多东西,但我相信这从根本上归结为我不能在任何异步肥皂函数中调用 this.foo()。虽然我可以将回调函数传递给createClient,如下所示:
function Thing(request, response) {
this.foo = function() {
console.log("this is foo");
}
this.sendSoap = function(err, client) {
// Get SOAP args
args = {
foo: this.request.cookies.foo <--- this.cookies undefined
};
client.doSomething(args, function(err, result) {
// Somehow call foo(); <--- CAN'T FIND A WAY TO DO THIS
});
}
this.doThing = function() {
// From the SOAP npm package library
soap.createClient(this.sendSoap);
}
}
// Make it so I can access this.request and this.response somehow
Thing.prototype = Object.create(AbstractThing);
我无法再访问 this.request.cookies,因为 this 现在在 sendSoap 闭包内被调用。我不知道为什么 javascript 将函数作为对象,但我有点沮丧。
我已经尝试了很多很多东西,但无法找到一种方法来做到这一点并且需要这样做,因为对 foo 的原始调用实际上是我在状态迭代器模式中使用的递归函数我用 Java 编写的 SOAP Web 服务中的身份验证。
我能想到的最后一种方法是修改 SOAP npm 包,以便我可以将 this.cookies 传递给 createClient 而不仅仅是回调。
我真的完全没有想法。任何帮助将不胜感激。
【问题讨论】:
标签: javascript node.js asynchronous soap callback