I am not sure why I get a Vue warning when accessing nested object.
{{ user.area.name }}
[Vue warn]: Error in render: "TypeError: Cannot read property 'name' of undefined"
TypeError: Cannot read property 'name' of undefined
Just accessing the object has no warning.
{{ user.name }}
Any advice?
Your
user
doesn't contain anarea
, so when you try to read that, it'sundefined
. You're not allowed to use the.
operator on things that areundefined
, so when you do.name
on that, you get the error that you got.Totally guessing here but lets see if I'm right...
Say you've got something like this in your component / Vue instance
data
initialiser...and you then populate that object asynchronously, eg
If your template has
when it initially renders before the asynchronous task has completed, you will be attempting to access the
name
property ofarea
which is undefined.Example ~ http://jsfiddle.net/tL1xbmoj/
Your options are...
Initialise your data with a structure that won't cause errors
Example ~ http://jsfiddle.net/tL1xbmoj/1/
Use conditional rendering to prevent the error
Example ~ http://jsfiddle.net/tL1xbmoj/2/