I'm starting with Javascript and I still do not handle the asynchronous topic well, promises ... etc
The question is, that within a callback I need to access the data of an array (traverse it through a for) and this throws me an error that I do not know how to fix.
My code:
var objetoQueGuardo = {'event': 'evento1', 'label': 'valor'};
var dataLayer = [];
dataLayer.push(objetoQueGuardo);
factura.createFactura(id, false, token)
.then(function successCallback(response) {
for (var i = 0; dataLayer.length; i++) {
dataLayer[i].label;
}
}, function errorCallback(response) {
console.warn(response);
});
I shortened the code a bit, so that example is simpler. The content of dataLayer is an array of objects and I want to keep the label attribute of each one.
The error that shows me:
TypeError: dataLayer[i] is undefined
Traza de la pila:
successCallback@http://api.local/components/api/assets/js/factura.js:150:7
h/<@http://api.local/components/api/assets/bower_components/angular/angular.min.js:135:278
$digest@http://api.local/components/api/assets/bower_components/angular/angular.min.js:146:238
$apply@http://api.local/components/api/assets/bower_components/angular/angular.min.js:149:464
Possibly unhandled rejection: {}