-2

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');
    }
}
Rémi
  • 1,313
  • 3
  • 20
  • 41
  • I don't understand what you mean in the first sentence. Can you explain what you mean with that? – Variable Apr 19 '17 at 13:52
  • 1
    You are calling callback potentially 3 times: once if `validateDates(from, to)` is false, and twice if `JSON.parse()` throws an error. – JJJ Apr 19 '17 at 13:53
  • My bad my question was unclear, I updated it, the error is in the async call not in the rest of the function. – Rémi Apr 19 '17 at 13:57

2 Answers2

0

I think you missing return statement in function.

When you catch or have an error instead of just callback() use return callback(). This will prevent execution of code bellow return statements and error that you see.

Hope this helps.

Mykola Borysyuk
  • 3,140
  • 1
  • 16
  • 23
  • My bad my question was unclear I have updated it, the error comes from the asycn.each call not the rest of the function. – Rémi Apr 19 '17 at 13:57
0

I think you have to call your async callback "done" without return:

done(null, {'circuitID': circuitID, data: timeWithPower});

and on error something like this:

done('errormessage');

so you get your result in the "final"callback after your each

see async

schaffioverflow
  • 500
  • 3
  • 14