0

Hello my problem is that i get undefined , when trying to accsess the array length, but everything works fine when i try to access only the array.

this above do not works ->

console.log(this.ref_number_response[0].info.length);

This works ->

console.log(this.ref_number_response);

and this is the whole

check_ref_number: function () {

 this.ref_number_response = [];

 axios.get('/is_referenceNumber_free/'+this.ref_number)
 .then(response => this.ref_number_response.push({ 
   info: response.data

}));


 console.log(this.ref_number_response[0].info.length);

 Event.$emit('reference_added', this.ref_number_response);

},
bhansa
  • 7,282
  • 3
  • 30
  • 55

2 Answers2

0

Emit the event after you recieve the data:

check_ref_number: function () {
 axios.get('/is_referenceNumber_free/'+this.ref_number)
 .then(response => Event.$emit('reference_added',[{info:response.data}]));
}

The problem is that you are getting the data asynchronously, and trying to use the data before it is ready.

Eladian
  • 958
  • 10
  • 29
0

You said you are trying to access the the array length, but

this.ref_number_response

is the array, so the only way this console.log(this.ref_number_response[0].info.length); is going to work ( you're trying to get the info property from first element of the array length not the actual array length ) is if info were an array as well. So you'd probably need to do something like:

console.log(this.ref_number_response.length);
Alejandro Camba
  • 978
  • 10
  • 25