所以最奇怪的事情正在发生。
我有这个node.js表达服务器,我想在启动时运行一个函数。
当它启动时,它会向我的数据库查询一组应该删除的对象,因为它们太旧了。我有一个for循环,我想用它来删除它们。但是当for循环开始并且我有一个变量(在这种情况下它现在应该被称为迭代器)设置为0,var iterator = 0
但它会一直设置为我的数组的长度。
因此,当for循环开始时,而不是迭代器等于0,它等于3.然后当循环发生时,它会中断,因为它试图访问data[3].ChildID;
以获取大小为3的数组,而不是data[0].ChildID;
导致这种情况的原因是什么?
app.listen(3000, function() {
console.log("Launch successful. To access app, open your browser and insert the following URL into your address bar: http://localhost:3000/");
// var i = 0;
// setInterval(updateDB.callUnenrolledList(function(err, data){
// if(err) {
// // do nothing
// }
// // send the data
// console.log("Checking for Unenrolled Children...");
// res.send(data);
// }), 1000);
updateDB.callUnenrolledList(function(err, data){
if(err) {
// do nothing
}
console.log("Checking for Unenrolled Children...");
if(data.length < 1){
console.log("Database is up to date. No Children Deleted");
//Do nothing. There are no unenrolled children
} else {
//Check to see if children have been "idle" for 5 years (arbitrary time set by SPCCC) and delete them if they have been
var currentDate = new Date();
var iterator = 0;
for(iterator; iterator < data.length; iterator++);{
console.log(iterator);
// var unenrolledDate = new Date(data[i].unenrolledDate);
var unenrolledDate = new Date("5/4/2017");
var difference = currentDate - unenrolledDate;
if(difference > 86400000){
console.log("Over 1 day old. Delete child!");
console.log(iterator);
var child = data[a].ChildID;
var ID = {ChildID: child};
updateDB.deleteChildFromDatabasePermanently(ID);
console.log("child deleted");
} else{
//do nothing
}
// if(difference > 157700000000){
// console.log("Over 5 years old. Delete child!");
// }
}
}
});
});
答案 0 :(得分:4)
您的 for循环行末尾放错了;
:)
尝试更改此内容:
for(iterator; iterator < data.length; iterator++);{
为:
for(iterator; iterator < data.length; iterator++) {
正如您所报告的那样,这个愚蠢的拼写错误地解释了iterator
等于data.length
。