【发布时间】:2018-12-25 13:50:14
【问题描述】:
我有一个循环,它最多对 API 端点进行五次调用以验证 ids。在完成循环之前的第一次迭代中,我的增量变量从0 变为1。
我指出,在控制台在请求之前记录变量之后,然后在每个回调中,无论请求是好是坏,都会发生这种情况。只要在.then 回调或.catch 回调中调用变量,索引就会增加,我不知道为什么。我测试了不同的变量名,仍然得到相同的结果。有人对此有想法吗?
我还在 React 中使用了 .fetch() 方法,同样的事情也发生在 .then 函数中,所以我认为这不是 axios 特有的。
这是我的功能:
isValidAIN(ains) {
var control = this;
var length = ains.length;
if (ains.length > 0) {
for (var i = 0; i < length; i++) {
if (ains[i].length !== 10) {
if (ains[i].length === 0) {
this.state.errors["ain[" + i + "]"] = "";
this.state.validAINS[i] = true;
} else {
this.state.errors["ain[" + i + "]"] = "This AIN Number Must Contain 10 Digits";
this.state.validAINS[i] = false;
}
this.setState(this.state);
} else {
// v this logs 0
console.log("i: ", i);
axios
.get("/myendpoint/?ain=" + ains[i])
.then((res) => {
// v this logs 1
console.log("in then: ", i);
console.log("res: ", res);
control.state.errors["ain[" + i + "]"] = "";
control.state.validAINS[i] = true;
control.setState(control.state);
})
.catch((err) => {
// v this logs 1
console.log("i in catch", i);
if (err.response.status == 404) {
control.state.errors["ain[" + i + "]"] = "AIN Is Invalid";
console.log(control.state.errors["ain[" + i + "]"]);
control.state.validAINS[i] = false;
control.setState(control.state);
return false;
}
});
// fetch("/myendpoint/?ain=" + ains[i])
// .then(res => res.json())
// .then(
// (result) => {
// console.log("success: ", result);
// if(result == null) {
// control.state.errors["ain[" + i + "]"] = "AIN Is Invalid";
// control.state.validAINS[i] = false;
// control.setState(control.state);
// return false;
// }
// else {
// control.state.errors["ain[" + i + "]"] = "";
// control.state.validAINS[i] = true;
// control.setState(control.state);
// }
// },
// (error) => {
// console.log("error: ", error);
// }
// )
// .catch(
// (err) => {
// console.log("Error from catch: ", err);
// }
// )
}
}
} else {
return false;
}
}
【问题讨论】:
-
不要不要直接修改状态,除非你在构造函数中。
-
axios请求是异步的,
var声明的变量的作用域是封闭函数。当 axios 请求完成时,这个变量将是循环的最大值,因为循环已经完成。将var更改为let以使i改为使用块范围。 -
啊哈@Tholle 非常感谢你成功了。
-
感谢 JakeG 和 Tholle 的快速修订
标签: javascript reactjs loops fetch axios