Error in render: "TypeError: Can not read property 'phone' of undefined"

1

I'm using Vue.js and I came across the title error.

I have an array object:

students[
          contact: {
            mobile: null,
            phone: null
          }
        ],

The contact array is not always populated.

How do I not give TypeError: Cannot read property 'phone' of undefined when the array has not been created?

I tried to add in more also did not give:

mounted () {
   this.students.contact.fone = ''}, 
    
asked by anonymous 14.08.2018 / 15:59

1 answer

-1

Attempts this.students['contact'].phone; : students is an array and contact is an array position with a JSON inside.

And to check try:

if(this.students['contact'].phone){
 /* phone existe */
}
    
17.09.2018 / 15:49