我使用javascript和nodejs創建項目。我在我的函數裏面爲循環集成了回調函數,但是我無法做到這一點。我的問題是回調在循環的第一次迭代完成。這裏是我的代碼:使用javascript進行回調
function tagEndpointNames(callback) {
var data = userGenerateToken();
var sql = "SELECT * FROM topology_data WHERE topology_coordinates !='' and topology_uuid is not null"
var query = conn.query(sql, function(err, tagEndpointNames) {
for (var i = 0; i < tagEndpointNames.length; i++) {
var topologytagData = {
"topology_tag": tagEndpointNames[i].topology_uuid
}
var tpCooridinates = JSON.parse(tagEndpointNames[i].topology_coordinates);
for (var j = 0; j < tpCooridinates.stageObjects.length; j++) {
if (tpCooridinates.stageObjects.length) {
if (tpCooridinates.stageObjects[j].endPointId) {
if (isGuid(tpCooridinates.stageObjects[j].endPointId)) {
var endPointUUID = tpCooridinates.stageObjects[j].endPointId;
var _ro = require('request');
var url = url;
var _d = '';
_ro({
url: url,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + data['access_token']
},
json: topologytagData
}, function(_e, _r, _b) {
if (_r.statusCode == 200 && !_e) {
callback()
//return;
} else {
callback()
console.log("andarss")
return;
}
})
}
}
}
}
}
})
}
下面是函數調用:
tagEndpointNames(function(){
console.log ('Server Closed during MIGRATION JOB 4');
server.close(function() {
process.exit(0);
});
})
你的代碼格式讓我的大腦出血,當我嘗試閱讀 –
我認爲你是在這兩個的if/else條件時,利用回調。 –
你是什麼意思'var url = url;'? –