[Vuejs]-Detecting an undefined/null object property in an aplication vue

1👍

Simply you don’t have expiration_date in method.data so first check if it exists then do your stuff.

var Data = (data.method && data.method.expiration_date) ? date.formatDate(String(data.method.expiration_date), 'DD/MM/YYYY') : '';

1👍

You need to add check before accessing properties of object, here i am using && operator ( short circuiting method ) to check existence of value before accessing it

var exp_data = data && data.methoda && data.method.expiration_date
var Data = (typeof exp_data !== undefined && typeof exp_data !== undefined ) ? date.formatDate(String(exp_data), 'DD/MM/YYYY') : ''

In you you need to test for any false value then you can simply use this

var exp_data = data && data.methoda && data.method.expiration_date
var Data = exp_data ? date.formatDate(String(exp_data), 'DD/MM/YYYY') : ''

1👍

You can use:

var Data = (data.method && data.method.expiration_date) ? date.formatDate(String(data.method.expiration_date), 'DD/MM/YYYY') : ''

Checks for existence of data.method and before referencing data.method.expiration_date.

Leave a comment