我有以下 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 调用的响应是不正确的,并且只得到一个值进入循环的结果。