I am getting this error:
node:19100) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Callback was already called.
On my async.each call, it seems like it is trying to call "done()" more then once per "circuit" but I don't understand why, i though that once the async callback is called the function would exit ?
Circuits is an array of String containing ids. I am simply trying to loop through them to execute async calls to database on each.
var getTimeseriesForCircuit = function(circuits, to, from, callback){
if (!validateDates(from, to)) {
return callback(400, 'Invalid date(s)');
}
var validJSON = false;
try {
circuits = JSON.parse(circuits);
validJSON = true;
}
catch (e) {
return callback(500, e);
}
if (validJSON) {
async.each(circuits, function (circuitID, done) {
var frequency = getFrequencyFromRange(from, to);
var influxFromDate = from * 1000000;
var influxToDate = to * 1000000;
getVoltageGatewayID(null, circuitID, function (gatewayID) {
getVoltageFromInflux(null, influxFromDate, influxToDate, gatewayID, frequency, function (voltage) {
getMeanAmpsFromInflux(null, influxFromDate, influxToDate, circuitID, frequency, function (data) {
if (JSON.stringify(data) != []) {
var timeWithPower = calculatePower(data, voltage);
return done(null, {'circuitID': circuitID, data: timeWithPower});
}
});
});
});
}, function (err, results) {
if (err) {
return callback(500, err)
} else {
return callback(200, results)
}
});
}
else {
return callback(400, 'The circuits sent were not in a valid format');
}
}