2016-03-30 34 views
1

我有场景。我想在mongodb中更新数据后执行循环。装置,如认为:如何让循环等待node.js中的回调?

var i = 0; 
while (i< 5) { 
    attendanceDataModel.update(query, condition).exec(function(error, data) { 
     if (error) { 
      console.log("Error @ 168 line in app.js File : \n" + err + "\n"); 
      i++; 
     } else { 
      if (data.length <= 0) { 
       console.log("No Records Matched."); 
       i++; 
      } else { 
       console.log(currEmpId + " : successfully Logged Out ! :-)" + data + "\n"); 
       updateRecordNumber(currRecordNumber); 
       i++; //wrong because it increases the value before updating in DB. 
      } 
     } 
    }); 
} 

var updateRecordNumber = function(currRecordNumber) { 

    var condition = { deviceLogId: parseInt(currRecordNumber) }; 

    lastDeviceLogIdModel.update({}, condition).exec(function(error, data) { 
     if (error) { 
      console.log("Error @ 213 line in app.js File : \n" + err + "\n"); 
     } else { 
      if (data.length <= 0) { 
       console.log("No Records Matched." + "\n"); 
      } else { 
       console.log(currRecordNumber + " : DeviceLogId successfully Updated ! :-)"); 
     // I want to increase value of i here after updation in database 
      } 
     } 
    }); 
} 

现在,我想增加变量i值之后,功能updateRecordNumber已成功更新

回答

1

简单的方法就是像var updateRecordNumber = function(currRecordNumber, callback)变化功能,然后更换调用:updateRecordNumber(currRecordNumber, function(){ i++ });

但我认为使用一些控制流方法是一个更好的解决方案,例如PromisesAsync.js

P.S.当然你必须改变函数的主体:

var updateRecordNumber = function(currRecordNumber, callback) { 
// all your async stuff 
    callback(); 
} 
+1

导致进入无限循环...... –

+0

@AmulyaKashyap是的。用recurent功能你应该改变while循环。函数foo(i){if(i == 5)return;/*你的循环中的东西* /; FOO第(i + 1);/*而不是i ++ * /}; ' –

+0

但是我推荐使用异步库从anwer –

0

你的while循环是同步的,并不考虑对数据库操作的响应有时候会稍后返回。 您需要通过递归重新安排操作(与降5次尝试后)的unsuscessful操作后重新查询:

function execute(count, callback) { 
    if (count == 5) { 
     return callback(new Error('meh...')) 
    } 

    loadSomethingAsync(function(error, result) { 
     if (error || !result) { 
      return execute(count++, callback) 
     } 

     callback(null, result) 
    }) 
} 

execute(0, function(error, result) { 
    if (error) { 
     return console.log('after 5 tries still no result or error...') 
    } 

    console.log('yay, async op has finished!') 
}) 
+0

请详细解释.... –

1

代码可以改为:

var i = 0; 

function startUpdation() { 

    return attendanceDataModel.update(query, condition).exec(function(error, data) { 
    if (error) { 
     console.log("Error @ 168 line in app.js File : \n" + err + "\n"); 
     i++; 
     if (i<5) { 
      return startUpdation(); 
     } 
     return; 
    } else { 
     if (data.length <= 0) { 
      console.log("No Records Matched."); 
      i++; 
      if (i<5) { 
       return startUpdation(); 
      } 
      return; 
     } else { 
      console.log(currEmpId + " : successfully Logged Out ! :-)" + data + "\n"); 
      return updateRecordNumber(currRecordNumber).then(function (err, data){ 
       i++; 
       if (i<5) { 
        return startUpdation(); 
       } 
       return; 
      }); 
     } 
    } 
}); 
} 

function updateRecordNumber (currRecordNumber) { 

var condition = { deviceLogId: parseInt(currRecordNumber) }; 

return lastDeviceLogIdModel.update({}, condition).exec(function(error, data) { 
    if (error) { 
     console.log("Error @ 213 line in app.js File : \n" + err + "\n"); 
    } else { 
     if (data.length <= 0) { 
      console.log("No Records Matched." + "\n"); 
     } else { 
      console.log(currRecordNumber + " : DeviceLogId successfully Updated ! :-)"); 
     } 
    } 
}); 
} 

startUpdation(); 

请尝试此解决方案。

1

这会更好,如果你promisify函数updateRecordNumber和编写增量调用在then()。

+0

我们可以在node.js部分中进行promisify。我的意思是这是一个后端代码 –

+0

当然。你可以使用蓝鸟库promisify。 http://bluebirdjs.com/docs/getting-started.html –

0

将回路重构为回调的一部分如何?事情是这样的:

var i = 0, 

    fna = function (error, data) { 
     if (error) { 
      console.log("Error @ 168 line in app.js File : \n" + err + "\n"); 
      fnc(); //i++; 
     } else { 
      if (data.length <= 0) { 
       console.log("No Records Matched."); 
       fnc(); //i++; 
      } else { 
       console.log(currEmpId + " : successfully Logged Out ! :-)" + data + "\n"); 
       updateRecordNumber(currRecordNumber); 
       //i++; //wrong because it increases the value before updating in DB. 
      } 
     } 
    }, 

    updateRecordNumber = function (currRecordNumber) { 
     var condition = { 
      deviceLogId : parseInt(currRecordNumber, 10) 
     }; 

     lastDeviceLogIdModel.update({}, condition).exec(fnb); 
    }, 

    fnb = function (error, data) { 
     if (error) { 
      console.log("Error @ 213 line in app.js File : \n" + err + "\n"); 
     } else { 
      if (data.length <= 0) { 
       console.log("No Records Matched." + "\n"); 
      } else { 
       console.log(currRecordNumber + " : DeviceLogId successfully Updated ! :-)"); 
       // I want to increase value of i here after updation in database 
       fnc(); 
      } 
     } 
    }, 

    fnc = function() { 
     i++; 

     if (i < 5) { 
      attendanceDataModel.update(query, condition).exec(fna); 
     } 
    }; 

attendanceDataModel.update(query, condition).exec(fna);