我有一个从一开始就_code设置为的类,null然后向 an 发出请求url以获取结果。
不知何故,我在分配了类的属性代码后,结果仍然给了我null。
我做错了什么?
class R {
constructor() {
this._code = null;
}
get code() {
return this._code;
}
set code(value) {
this._code = value;
}
async makingRequests(id) {
await this.requestToGetCode(id);
// this gives me null
console.log(this.code, 'this.code in rquest');
}
async requestToGetCode(id) {
await request(url, async (error, response, body) => {
if (body !== 'found_no_results') {
switch (response.statusCode) {
case 200:
this.code = await JSON.parse(body);
// this does give me the proper result though
console.log(this.code, 'this.code in requestToGetCode');
break;
case 404:
console.log('page not found');
break;
default:
break;
}
} else {
console.log(body, id);
}
});
}
}
提前感谢您的任何帮助和建议。
守着星空守着你
相关分类