-2

Detecting an undefined/null object property in an aplication vue.

I can not resolve this object problem not defined.

I am using a vue application with esLint. But I believe this is only solved with javascript.

Thank you guys.

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

Error:

Uncaught (in promise) TypeError: Cannot read property 'expiration_date' of null
1

3 Answers 3

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') : '';
Sign up to request clarification or add additional context in comments.

Comments

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') : ''

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.