【发布时间】:2019-12-11 13:07:14
【问题描述】:
我正在尝试使用请求中的回调方法进行 api 调用,但我是 web 开发的新手,目前我被困在 async 上。我有以下工作,但我想打破一些逻辑。例如,这是目前正在运行的内容
const request = require('request');
class GetAllData{
constructor(){
this.loadData();
}
// Gets all data from api query that I'll need to build everything else I'll need
data(callback){
request({'url':`https://definitely/a/url.json`, 'json': true }, function (error, response, body) {
callback(body);
});
}
loadData(cmrUrl){
console.log("loadData");
this.data(function(result){ console.log(result.foo.bar)});
}
}
var moreData = new GetAllData();
这行得通,我可以做我需要做的两件事,即记录一些结果,并对结果进行小计算。但是,如果我想将此逻辑分解为其他函数,则会出现一些错误。
const request = require('request');
class GetAllData{
constructor(){
this.loadData();
// Member variables
this._subsetOne;
this._thingICalculated;
// Function call to print data outside of the async call.
this.printData(this._subsetOne, this._thingICalculated);
}
// Gets all data from api query that I'll need to build everything else I'll need
data(callback){
request({'url':`https://definitely/a/url.json`, 'json': true }, function (error, response, body) {
callback(body);
});
}
loadData(cmrUrl){
console.log("loadData");
// Set a class member variable
// ERROR: 'this' is undefined
this.data(function(result){ this._subsetOne = result.foo.bar)};
// Call a member function, which calculates something, and then sets a member variable.
this.calculateSomething = result;
console.log(result);
};
}
// Function which takes in result from async call, then calculates something.
set calculateSomething(result){
this._thingICalculated = result + 1;
}
printData(x, y){
console.log(x,y);
}
}
var moreData = new GetAllData();
从我一直在阅读的内容来看,我遇到的问题很常见,但我仍然不明白为什么这不起作用,因为调用是异步的,我只是想设置一个变量,或调用函数。我假设有一些方法可以要求成员变量设置和函数调用来等待异步请求的完成?
修复一次尝试
const request = require('request');
class GetAllData{
constructor(){
this.loadData();
this._subset;
}
// Gets all data from api query that I'll need to build everything else I'll need
data(callback){
request({'url':`https://definitely.a.url/yep.json`, 'json': true }, function (error, response, body) {
callback(body);
});
}
loadData(cmrUrl){
console.log("loadData");
this.data(function(result){ this._subset = result
this.getSubset()}.bind(this));
}
getSubset(){
console.log(this._subset);
}
}
var moreData = new GetAllData();
子集最终未定义。
【问题讨论】:
-
this未定义,因为它被称为:callback(body)。您需要使用apply或call来设置this。 -
我不太明白你在问什么,但是在构造函数中使用异步操作(看起来你可能正在做)需要不同的设计模式才能让代码知道当那些异步的事情完成时。有关执行此操作的各种设计模式,请参阅 Asynchronous operations in a constructor。
-
您是如何使用 setter 将回调传递给请求的?您在某处的示例中看到了吗?
-
谢谢,只是好奇;我也没见过。这对我来说似乎有点令人费解,但这并不是真正的问题。
-
现在你遇到了不同的问题。在您调用
moreData.getSubset()时,甚至还没有调用异步函数的回调函数。我建议您阅读:How do I return the response from an asynchronous call?
标签: javascript node.js asynchronous callback request