我正在尝试在我用来将数据拉入我的反应应用程序的 javascript 帮助程序类中按环境设置 API 端点。它看起来像这样:
import axios from 'axios';
class QueryHelper {
endpoints;
SupportEndpoint;
MemberEndpoint;
constructor() {
debugger
// get the endpoints set
fetch('/environment')
.then(response => response.json())
.then(data => this.endpoints = data) // set the endpoints var with the json payload with all the endpoint and env data
.then(() => {
this.SupportEndpoint = this.endpoints.supportBaseUrl;
this.MemberEndpoint = this.endpoints.memberBaseUrl;
});
}
async fetchData(resource) {
const resp = await axios.get(this.SupportEndpoint + '/' + resource);
return resp.data;
}
}
export default QueryHelper;
它会像这样使用:
let helper = new QueryHelper();
helper.fetchData('MemberProfile')
.then(response => response.json())
.then(//do some other stuff);
当我到达断点时,我可以单步执行构造函数,并且各个端点似乎都按预期设置。但是当fetchData
调用该方法时,SupportEndpoint
(和任何其他端点)是undefined
并且 ajax 调用失败并出现 404。