This error is because this
is not referrring to GetAPI
component and that's because the way you have nested multiple functions inside componentDidMount
function.
Write all those functions outside the componentDidMount
function and use arrow functions.
Not related to the error but you are also not updating the state correctly. You are overwriting it instead of appending the new url to the existing list of urls.
Change
this.setState({ Url: resp.data.Url });
to
this.setState({ Url: [...this.state.Url, resp.data.Url] });
Here's the fixed code
class GetAPI extends Component {
constructor(props) {
super(props);
this.state = {
ID: [],
Url: [],
};
}
componentDidMount() {
console.log(this.props.exptype);
var accesstoken;
let reports = [];
axios
.post(API, qs.stringify(requestBody), config)
.then((result) => {
console.log(result);
this.setState({ token: result.data.access_token });
accesstoken = result.data.access_token;
console.log("access token ins " + accesstoken);
this.invokeGetReport(accesstoken);
})
.catch((error) => {
console.log(error);
console.log(error.data);
});
console.log("repoddrts sdda " + reports[1]);
console.log("prop value is " + this.props);
// console.log("report id " + this.state.ID);
}
invokeGetReportDetails = (accesstoken, reportID) => {
console.log("reportID in detail API is " + reportID);
const config_req = {
headers: {
Accept: "application/json",
Authorization: "Bearer " + accesstoken,
},
};
axios
.get(report_details + reportID, config_req)
.then((resp) => {
console.log(resp);
console.log(
"report data id is " + resp.data.ExpenseEntriesList.length
);
for (let i = 0; i < resp.data.ExpenseEntriesList.length; i++) {
let expenseEntryId = resp.data.ExpenseEntriesList[i].ReportEntryID;
let ExpenseTypeName =
resp.data.ExpenseEntriesList[i].ExpenseTypeName;
console.log(
"id is : " +
expenseEntryId +
" ExpenseTypeName : " +
ExpenseTypeName
);
this.invokeImageURL(accesstoken, expenseEntryId);
}
})
.catch((error) => {
console.log(error);
console.log(error.data);
});
}
invokeImageURL = (accesstoken, expenseEntryId) => {
console.log("expenseEntryId in detail API is " + expenseEntryId);
const config_req = {
headers: {
Accept: "application/json",
Authorization: "Bearer " + accesstoken,
},
};
axios
.get(image_url + expenseEntryId, config_req)
.then((resp) => {
console.log(resp);
console.log("URL is " + resp.data.Url);
this.setState({ Url: [...this.state.Url, resp.data.Url] });
})
.catch((error) => {
console.log(error);
console.log(error.data);
});
}
invokeGetReport = (accesstoken) => {
console.log("access token is " + accesstoken);
const config_req = {
headers: {
Accept: "application/json",
Authorization: "Bearer " + accesstoken,
},
};
axios
.get(test_report_url, config_req)
.then((resp) => {
console.log(resp);
console.log("data id is " + resp.data.Items.length);
// debugger;
for (let i = 0; i < resp.data.Items.length; i++) {
let reportName = resp.data.Items[i].Name;
let reportID = resp.data.Items[i].ID;
console.log("id is : " + reportID + "Report Name : " + reportName);
this.invokeGetReportDetails(accesstoken, reportID);
reports.push(reportID);
}
// invokeGetReportDetails(accesstoken, reports);
})
.catch((error) => {
console.log(error);
console.log(error.data);
});
}
render() {
return (
<div>
<DownloadButton imageUrl={this.Url} />
</div>
);
}
}
export default GetAPI;
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…