我有以下api:
for (var index in workload.elements) {
(function(index) {
var arr = [];
var resourceIdentifiers = {};
var elementinfo = {};
var metadataModified = {};
elementinfo = workload.elements[index];
arr[index] = workload.elements[index].uri;
if (workload.elements[index].parameters.imageUri) {
arr.push(workload.elements[index].parameters.imageUri);
}
resourceIdentifiers = arr.join(',');
console.log('uri' + resourceIdentifiers);
mysql.elementlevelpricing(resourceIdentifiers, function(result) {
elementlevelpricingSummary = result;
metadataModified = workload.elements[index].metadata;
metadataModified.pricingsummary = elementlevelpricingSummary;
delete elementinfo.metadata;
elementinfo.metadata = metadataModified;
workloadinfo.elements = JSON.stringify(elementArray, null, 2);
elementArray[index] = elementinfo;
console.log(JSON.stringify(elementArray, null, 2));
res.send(JSON.stringify(elementArray, null, 2));
});
})(index);
}
console.log打印出正确的结果,但对REST调用的响应不正确,只得到一个值进入循环的结果。