Wednesday, 24 May 2017

Error: Callback was already called

I'm having troubles trying to fix my code with callbacks... So I have a for loop because I have a configuration called mwan0, mwan1 and mwan2 so i figured the easy way to do this was to do a for loop and pass the i value to the string and concat. after I pass this as a argument to a function..

LogsController.prototype.pollingServicesInterfaces = function () { createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'pollingServicesInterfaces', 'inicio'); var numDevices = 1; return Database.Devices.getDevices().then(function (devices) { return new Promise(function (resolve, reject) { async.each(devices, function (device, callback) { console.log("pollingServicesInterfaces device ",device); console.log("pollingServicesInterfaces antes do for"); for(var i=0;i<3;i++){ console.log("pollingServicesInterfaces depois do for"); var radio="mwan"+i; var wifi="radio0"; console.log("pollingServicesInterfaces antes de entrar Controllers.Devices.getRadioInfo 1"); Controllers.Gps.getRadioInfo('uci', 'get', {"config": "network", "section":radio}, device).then(function (data) { var service={ "id":uuid.v4(), "interface": { "ifname":data.values.ifname, "enabled":data.values.enabled } "status":true }; console.log("pollingServicesInterfaces entrei no Controllers.Devices.getRadioInfo"); //if data.disabled=0 desligado senĂ£o if data.disabled=1 esta ligado console.log("pollingServicesInterfaces mwan"+i+" ifName -->",data.values.ifname); console.log("pollingServicesInterfaces mwan"+i+" enabled -->",data.values.enabled); return Database.Services.addRow(service, device); }).then(function() { return callback(); }).catch(function (e) { createLog('error', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'pollingServicesInterfaces', 'no callback 1',e); return callback(e); }); } }, function (error) {

            if (error) {
                createLog('error', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'pollingServicesInterfaces', 'stops polling?!', error);
                return reject(error);
            }
            createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'pollingServicesInterfaces','All Devices Processed');
            return resolve();
        });
    });
}).catch(function (e) {
    createLog('error', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'pollingServicesInterfaces','FATAL',e);
});

}



via Catia Matos

No comments:

Post a Comment