0

The asynchronous callback within for loop of node js creates issue:

for(var index in workload.elements) {
    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.newitem = elementlevelpricingSummary;

        delete elementinfo.metadata;

        elementinfo.metadata = metadataModified;

        console.log('resultout' + JSON.stringify(elementinfo,null,2));

    });
}

In the above code , the workloads.elements is the array containing more than one value. But i get callback result for only one of the values.

Matt Cain
  • 5,638
  • 3
  • 36
  • 45
Prem
  • 5,685
  • 15
  • 52
  • 95
  • 2
    This is one of those zillions :-) : http://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example – go-oleg Jul 31 '13 at 18:18
  • yes. i got the solution from the mentioned threads. – Prem Jul 31 '13 at 18:44

0 Answers0