我想知道是否有人可以看到这个for循环的问题。
我正在使用解析,for
循环位于对象查询的success
回调中。
当循环运行时,i
会在它完成第一个循环之前跳转到response.length
var e = [];
for (i = 0; i < response.length; i++) {
console.log("length: " + response.length);
var query = new Parse.Query(conEvent.Events);
query.get(response[i].get("eventID"), {
success: function (result) {
var object = result;
console.log("i: " + i)
e[i] = {
"name": object.get("name"),
"description": object.get("description"),
"dates": conEvent.datesToArray(object.get("dates")),
"ufDates": object.get("dates"),
"creator": object.get("creator"),
"id": object.id,
"invited": conEvent.getInvited(object.id)
}
console.log(e);
},
error: function (error) {
alert("Error: " + error.code + " " + error.message);
}
});
}
目前:
console.log("length: " + response.length);
输出“length:2”
console.log("i: " + i)
输出“i:2”
如果有人需要,循环的更多上下文视图:
this.getEvents = function () {
//get all the events user is invited to, list them and list the last....3? actions on the event
//display most likely date
//display number of users voted
//go green when date chosen
console.log("getEvents");
var Invite = Parse.Object.extend("Invite");
var query = new Parse.Query(Invite);
query.equalTo("username", Parse.User.current().get("username"));
query.find({
success: function (response) {
var e = [];
for (i = 0; i < response.length; i++) {
console.log("length: " + response.length);
var query = new Parse.Query(conEvent.Events);
query.get(response[i].get("eventID"), {
success: function (result) {
var object = result;
console.log("i: " + i)
e[i] = {
"name": object.get("name"),
"description": object.get("description"),
"dates": conEvent.datesToArray(object.get("dates")),
"ufDates": object.get("dates"),
"creator": object.get("creator"),
"id": object.id,
"invited": conEvent.getInvited(object.id)
}
console.log(e);
},
error: function (error) {
alert("Error: " + error.code + " " + error.message);
}
});
}
conEvent.myEvents = e;
console.log(e);
$scope.$apply();
}
});
}