I have a problem in my Node.js project. I trying to return the "Report" object after the for loop finished. my code(Updated):
var apigClient = apigClientFactory.default.newClient({
accessKey: '*******',
secretKey: '*******',
invokeUrl: '*******'
});
var pathTemplate = '*******';
var method = 'POST';
///loop - calling API
for (var i = 0; i < Object.keys(jsonOutput).length; i++)
{
var body = {
*******: *******,
*******: *******
};
apigClient.invokeApi({}, pathTemplate, method, {}, body)
.then(function (result) {
Report.numberOfSuccess++;
console.log(JSON.stringify(result.data));
}).catch(function (result) {
Report.numberOfFailed++;
Report.reportList.push([jsonOutput[i].id,jsonOutput[i].currency]);
});
}
/////finally
console.log(Report);
my output:
{ reportList: [], numberOfSuccess: 0, numberOfFailed: 0 }
{ reportList: [], numberOfSuccess: 0, numberOfFailed: 0 }
{ reportList: [], numberOfSuccess: 0, numberOfFailed: 0 }
{"status":500,"error":"Internal Server Error"}
{"status":500,"error":"Internal Server Error"}
{"status":500,"error":"Internal Server Error"}
{"status":500,"error":"Internal Server Error"}
{"status":500,"error":"Internal Server Error"}
{"status":500,"error":"Internal Server Error"}
{"status":500,"error":"Internal Server Error"}
{"status":500,"error":"Internal Server Error"}
{"status":500,"error":"Internal Server Error"}
As you can see the the Report object is not ready.
what I need is:
{"status":500,"error":"Internal Server Error"}
{"status":500,"error":"Internal Server Error"}
{"status":500,"error":"Internal Server Error"}
{"status":500,"error":"Internal Server Error"}
{"status":500,"error":"Internal Server Error"}
{"status":500,"error":"Internal Server Error"}
{"status":500,"error":"Internal Server Error"}
{"status":500,"error":"Internal Server Error"}
{"status":500,"error":"Internal Server Error"}
{ reportList: [], numberOfSuccess: 4, numberOfFailed: 0 }
{ reportList: [], numberOfSuccess: 3, numberOfFailed: 0 }
{ reportList: [], numberOfSuccess: 2, numberOfFailed: 0 }
I'v tried to use promise and callback but it isn't works.