user2925894 user2925894 - 1 month ago 4
AngularJS Question

Can't get information from my table in angularJS

I am pushing a value into a table. But when i try to print it out in my

console.log
I get undefined, but I know I'm pushing a value that is not undefined.

var attachmentIds = [];
for(var i = 0; i < files.length; i++){
uploadFileToServer(files[i]).then(function(data){
console.log(data[2]);
if(data[0]){
completed();
}else if(!data[0]){
completed();
uploadFileStatus.push({file: data[1].name, uploaded: data[0], message: data[2]});
}
attachmentIds.push(data[2]);
console.log(attachmentIds[i]);
});
};



[10:53:51.492] ""533d219fff0006d40ced67ab""

[10:53:51.493] undefined


Here is my 2 console logs

Answer

You are manipulating attachmentIds inside then(). This is handled asynchron. So you dont know which value i has when logging attachmentIds[i]. Try logging the whole array attachmendIds and you should see your data.