我正在开发一个React项目,我正在使用Mobx进行商店销售。我正在使商店正常工作,我正在毫无问题地保存数据。我遇到的一个问题是我调用了几个函数,似乎它们正在向后运行,我无法弄清楚。如果有人可以用新鲜的眼睛看一下代码并告诉我我所缺少的内容,我将在下面列出代码。
.js主文件
componentDidMount(){
const query = parse(location.search);
if (query.slug !== undefined) {
this.props.IftaStore.getTruck(query.slug);
this.props.IftaStore.load();
} else {
this.props.IftaStore.setLimitByTruck(false);
this.props.IftaStore.load();
}
}
我基本上有一个被称为slug的网址。我分析说,如果不是不确定的,我在商店中调用一个名为getTruck的函数(见下文)。一旦运行getTruck,则应调用函数load(也在商店中)。似乎正在发生的事情是先运行负载,然后运行getTruck。
商店代码:
getTruck = async user => {
const thisConst = this;
let endpoint = '/api/equipment/truck/?driver=' + user;
let lookupOptions = {
method: "GET",
headers: {
'Content-Type': 'application/json'
}
};
let data = [];
try {
const response = await fetch(endpoint, lookupOptions);
const data = await response.json();
thisConst.trucknumber = data.results[0]["trucknumber"];
thisConst.limit_by_truck = true;
} catch (e) {
console.log(e);
}
console.log("getTruck trucknumber: ", thisConst.trucknumber);
if (data.count === 0) {
alert("You are not assigned to a truck, please contact your Driver Manager")
}
};
load = async nextEndpoint => {
const thisConst = this;
let data = [];
let endpoint = '/api/dispatch/ifta_stand/';
let lookupOptions = {
method: "GET",
headers: {
'Content-Type': 'application/json'
}
};
console.log("load truck number: ", thisConst.trucknumber);
if (thisConst.limit_by_truck) {
endpoint = '/api/dispatch/ifta_stand/?truck_number=' + thisConst.trucknumber;
} else {
endpoint = '/api/dispatch/ifta_stand/'
}
if (nextEndpoint !== undefined) {
endpoint = nextEndpoint;
}
try {
const response = await fetch(endpoint, lookupOptions);
const data = await response.json();
thisConst.ifta_stand = data.results;
thisConst.next = data.next;
thisConst.previous = data.previous;
thisConst.count = data.count;
} catch(e) {
console.log(e);
}
};
当我运行此应用程序时,控制台日志如下:
iftaStore.js:138 load truck number: 0
iftaStore.js:100 getTruck trucknumber: 24
相反,getTruck卡车编号应该在装载卡车编号之前记录。
有人可以看看我的代码,让我知道我所缺少的...
非常感谢您抽出宝贵的时间。