2

I have a property that comes from axios.get.

It's defined in my data as null (or '' or [])

The axios call is in a created hook.

When the app is first loaded the console fills up with

 Error in render: "TypeError: Cannot read property 'service' of null"

Of course, it actually shows the information correctly on the webpage, because service is not null, but this error is apparently thrown before axios fetches the data.

What's the best approach here?

1
  • Can you please share some code? It'll help us figure out what's wrong. Commented Mar 8, 2019 at 1:00

3 Answers 3

6

You have to specify your data as an empty object to avoid this error:

    data: {
       your_object: {}
   }

Where your_object is that object that will have service property when data is loaded.

(From my assumption, in axios.get promise function you do something like that:

your_object.service = result.data;

)

Sign up to request clarification or add additional context in comments.

Comments

4

add a v-if in the element where you are using the data service.

Here is an example:

<label v-if="service">{{service}}</label>

or use the parent if it's an array like this:

<label v-if="parent">{{parent.service}}</label>

Comments

3

You can either initialize the data field with an empty, non null, value or guard the access using a v-if before the template using the property.

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.