Vue JS returns [__ob__: Observer] data instead of my array of objects

后端 未结 11 785
予麋鹿
予麋鹿 2020-12-08 08:54

I\'ve created a page where I want to get all my data from the database with an API call, but I\'m kinda new to VueJS and Javascript aswell and I don\'t know where I\'m getti

相关标签:
11条回答
  • 2020-12-08 09:24

    You can use v-if directive to render the component once the data is there.

    <h3 v-if="pigeons.length > 0">{{ pigeon.id }}</h3>
    
    0 讨论(0)
  • 2020-12-08 09:25

    Can also try this:

    var parsedobj = JSON.parse(JSON.stringify(obj))
    console.log(parsedobj)
    

    It was brought by Evan You himself here and more info on that here

    But waiting for the answer is a first step.

    0 讨论(0)
  • 2020-12-08 09:26

    Well I had exactly same problem but I solved it:

    Just move app.js external link to head tag.

    0 讨论(0)
  • 2020-12-08 09:34

    The best way possible

    obj2 = JSON.stringify(obj)
    obj3 = JSON.parse(obj2)
    

    Or

    obj2 = Object.assign([], obj)
    

    It was brought by Evan You himself here and more info

    0 讨论(0)
  • 2020-12-08 09:38

    This happens because Vue js convert every item in the data to something that can be observed. So it makes sense if you console log something in the data. The output will be something wrapped into an observer.

    To have a better vision on your data I suggest you to install the Vue dev tools. https://chrome.google.com/webstore/detail/vuejs-devtools/nhdogjmejiglipccpnnnanhbledajbpd?hl=en

    0 讨论(0)
  • 2020-12-08 09:39

    You should probably wait for the fetch to finish then console.log the result ..

    created(){
        this.fetchPigeons().then(() => console.log(this.pigeons));
    },
    

    The way you were doing it you were logging the result synchronously so it gets executed before the fetch is done.

    Edit: Also as @barbsan pointed out in his comment below your fetchPigeons needs to return a promise for then to work. fetch returns a promise so you just need to return fetch in fetchPigeons

    fetchPigeons(){
        return fetch('api/racingloft')
        .then(res => res.json())
        .then(res => {
            console.log(res.data); // Here I get what I need
            this.pigeons = res.data;
        })
    }
    
    0 讨论(0)
提交回复
热议问题