【发布时间】:2018-06-21 15:51:16
【问题描述】:
我正在调用 this.getAllJobsAPI();在渲染函数中。
这成功调用了 getAllJobsAPI(如下所示),我能够从 JSON 作业数组中检索作业名称。
getAllJobsAPI(){
var headers = new Headers();
headers.append('Authorization', 'Basic YWRtaW46YWRtaW4=');
fetch('http://localhost:8080/api/json', {headers: headers})
.then((result) => {
// Get the result
// If we want text, call result.text()
return result.json();
}).then((jsonResult) => {
// get all jobs
var jobs = [];
Object.keys(jsonResult.jobs).forEach(function(key) {
jobs.push(jsonResult.jobs[key].name);
var job = jsonResult.jobs[key].name;
console.log(job);
this.getLastBuildAPI(job);
});
//var a = 'meetup';
//this.getLastBuildAPI(a);
console.log(jsonResult);
})
}
但是调用 this.getLastBuildAPI(job);在 for each 循环中抛出“Uncaught (in promise) TypeError: Cannot read property 'getLastBuildAPI' of undefined”错误。但是如果我在函数末尾硬编码一个名称变量并调用 this.getLastBuildAPI(a);我可以得到成功响应。
谁能告诉我为什么我不能调用 this.getLastBuildAPI(job);在每个 JSON 数组中?请注意,我可以在控制台中看到作业变量。
谢谢,
【问题讨论】:
标签: javascript json reactjs typescript typeerror