Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue.js cannot access to nested properties of data object

In Vue.js I fetch some data of a json-file like this:

export default {
    data () {
       return {
           data: []
       }   
    },
    created () {
        this.fetchData();    
    },
    methods: {
        fetchData () {
            $.getJSON('data/api.json', function(el) {
                this.data = el;
            }.bind(this)),
        }
    }
}

The fetched data has the following structure:

{
    time: '17:00',
    pick: {
        box: {
            single: 1,
            multi: 2
        }    
    }
}

When I try to access to {{ data.pick.box }} or {{ data.pick.box.single }} in my component, I always get this error message:

vue.js?3de6:2963 Uncaught TypeError: Cannot read property 'box' of undefined
at Proxy.render (eval at <anonymous> (app.js:126), <anonymous>:4:46)
at VueComponent.Vue._render (eval at <anonymous> (app.js:139), <anonymous>:2954:22)
at VueComponent.eval (eval at <anonymous> (app.js:139), <anonymous>:2191:21)
at Watcher.get (eval at <anonymous> (app.js:139), <anonymous>:1656:27)
at new Watcher (eval at <anonymous> (app.js:139), <anonymous>:1648:12)
at VueComponent.Vue._mount (eval at <anonymous> (app.js:139), <anonymous>:2190:19)
at VueComponent.Vue$3.$mount (eval at <anonymous> (app.js:139), <anonymous>:5978:15)
at VueComponent.Vue$3.$mount (eval at <anonymous> (app.js:139), <anonymous>:8305:16)
at init (eval at <anonymous> (app.js:139), <anonymous>:2502:11)
at createComponent (eval at <anonymous> (app.js:139), <anonymous>:4052:9)

Is there any restriction for accessing deep nested objects? When I call {{ data }}, for example, I get the whole data structure displayed as a string correctly.

As mentioned by Nora, here is the fiddle: jsfiddle

like image 390
Sebastian Rush Avatar asked Jan 02 '17 09:01

Sebastian Rush


Video Answer


1 Answers

My solution was to create an empty object with empty properties.

 data () {
       return {
           loading: false,
           data: {pick:{},}
       }   
    },
like image 146
Tax Raoul Avatar answered Oct 18 '22 01:10

Tax Raoul