我正在使用节点js和net-snmp模块构建linux服务器监控。这部分是用于检查已存储在mysql数据库中的OID的服务的脚本。我遇到问题,这个脚本总是只执行最新的循环。我不知道为什么。这是我的代码:
connection.query("select * from SERVICE WHERE SERVER_ID = ? order by SERVICE_LIST_ID", [result[i].SERVER_ID], function(err2, services) {
if (err2) throw(err2);
//tapping services
for(var j in services){
session.get ([services[j].SERVICE_OID], function (error, varbinds) {
if (error) {
console.error (error);
} else {
//save services
console.log(j);
var post = {LOG_ID: result_logr.insertId, SERVICE_ID: services[j].SERVICE_ID, SERVICE_STATUS: 1};
var query = connection.query('INSERT INTO LOG_SERVICE SET ?', post, function(err, result) {
if (err) {
connection.rollback(function() {
throw err;
});
}
connection.commit(function(err) {
if (err) {
connection.rollback(function() {
throw err;
});
}
console.log('Transaction Complete.');
//connection.end();
});
});
console.log(query.sql);
}
});
}
});
结果显示:
1
INSERT INTO LOG_SERVICE SET `LOG_ID` = 115, `SERVICE_ID` = 2, `SERVICE_STATUS` = 1
1
INSERT INTO LOG_SERVICE SET `LOG_ID` = 115, `SERVICE_ID` = 2, `SERVICE_STATUS` = 1
因此,它只执行j = 1。 请帮我。感谢
答案 0 :(得分:0)
也许异步问题,因为它将被并行执行。
尝试这个:
async = require('async')
async.eachSeries(services, function(elem,callback){
//Your logic here
AFter that
callback(); //to start the next loop
})