Vue JS返回[__ob__:观察者]数据而不是我的对象数组

时间:2018-10-18 12:00:37

标签: javascript arrays laravel vue.js axios

我已经创建了一个页面,我想通过API调用从数据库中获取所有数据,但是我对VueJS和Javascript也很陌生,我也不知道我在哪里弄错了。我确实使用Postman进行了测试,并获得了正确的JSON。

这就是我得到的:

[__ob__: Observer]
length: 0
__ob__: Observer {value: Array(0), dep: Dep, vmCount: 0}
__proto__: Array

这就是我想要的:

(140) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, …]
[0 … 99]
[100 … 139]
length: 140
__ob__: Observer {value: Array(140), dep: Dep, vmCount: 0}
__proto__: Array

那是我的Vue模板文件:

<template>
    <div>
        <h2>Pigeons in the racing loft</h2>
        <div class="card-content m-b-20" v-for="pigeon in pigeons" v-bind:key="pigeon.id">
            <h3>{{ pigeon.id }}</h3>
        </div>
    </div>
</template>

<script>
export default {
    data(){
        return{
            pigeons: [],
            pigeon: {
                id: '',
                sex: '',
                color_id: '',
                pattern_id: '',
                user_id: '',
                loft_id: '',
                country: '',
                experience: '',
                form: '',
                fatique: ''
            },
            pigeon_id: ''
        }
    },
    created(){
        this.fetchPigeons();
        console.log(this.pigeons); // Here I got the observer data instead my array
    },

    methods: {
        fetchPigeons(){
            fetch('api/racingloft')
            .then(res => res.json())
            .then(res => {
                console.log(res.data); // Here I get what I need
                this.pigeons = res.data;
            })
        }
    }
}
</script>

我也尝试过使用axios来做到这一点,但是它给了我完全相同的东西。当我从方法中进行控制台操作时,它提供了我的数据,但是在外部它什么也没有提供。

12 个答案:

答案 0 :(得分:5)

发生这种情况是因为Vue js将数据中的每个项目都转换为可以观察到的东西。因此,如果您在数据中控制台记录某些内容,这是有意义的。输出将包装到观察者中的东西。

为了更好地了解您的数据,建议您安装Vue开发工具。 https://chrome.google.com/webstore/detail/vuejs-devtools/nhdogjmejiglipccpnnnanhbledajbpd?hl=en

答案 1 :(得分:3)

您可能应该等待提取完成,然后console.log结果..

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

您执行操作的方式是同步记录结果,以便在提取之前执行结果。

编辑:另外,正如@barbsan在其下方的评论中指出的那样,您的fetchPigeons需要返回保证then正常运行的承诺。 fetch返回一个承诺,因此您只需要返回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;
    })
}

答案 2 :(得分:3)

基本上,您所做的是该函数获取鸽子数组,但是当它并行运行时,您调用了 console.log。轻松修复:

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

答案 3 :(得分:2)

也可以尝试以下方法:

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

它是由Evan You本人带来的here

但是等待答案是第一步。

答案 4 :(得分:2)

这是我的解决方法:

在$ root组件中添加类似log之类的新方法。推荐使用App.vue的{​​{1}}:

created

只需在组件中调用this.$root.log = function log() { for (let i = 0; i < arguments.length; i += 1) { if (typeof (arguments[i]) === 'object') { try { arguments[i] = JSON.parse(JSON.stringify(arguments[i])); } catch (e) { console.error(e); } } } console.log(...arguments); };

我的结果:

之前:

enter image description here

之后:

enter image description here

答案 5 :(得分:1)

感谢您的所有建议。只需使用“ const ”,事情就对我有利。

const cookieInfo = ToolCookieService.getToolNumbersFromCookie();

谢谢, 兰吉特

答案 6 :(得分:0)

一旦数据存在,您就可以使用v-if指令来呈现组件。

<h3 v-if="pigeons.length > 0">{{ pigeon.id }}</h3>

答案 7 :(得分:0)

如Husam Ibrahim所述,您应该等待异步fetch()函数来解决。另一种方法是在函数中使用async / await:

methods: {
    async fetchPigeons(){
        await fetch('api/racingloft')
               .then(res => res.json())
               .then(res => {
                   console.log(res.data);
                   this.pigeons = res.data;
               })
    }
}

然后它应该起作用:

<template>
  <div>
    <h2>Pigeons in the racing loft</h2>
    <div class="card-content m-b-20" v-for="pigeon in pigeons" v-bind:key="pigeon.id">
        <h3>{{ pigeon.id }}</h3>
    </div>
  </div>
</template>

答案 8 :(得分:0)

我仍然遇到同样的问题...

根据Evan(Vue作者)JSON.parse(JSON.stringify(res.data))方法编写这些文件,但是为什么不能正常显示数据?

  methods: {
    async getdata() {
      console.log(1);
      await axios.get(API).then((res) => {
          console.log(2);
          if (!this.dataList.length) {
            this.dataList = JSON.parse(JSON.stringify(res.data));
            console.log(3);
            console.log(res.data, 'res.data ');
            console.log(this.dataList, 'this.dataList')
            console.log('----------check-----------',JSON.parse(JSON.stringify(res.data)));
          }
        })
        .catch(err => {
          console.error('failed: ' + err);
        })
      console.log(4);
    },

答案 9 :(得分:0)

如果您的目标是调试Observer Vue实例中包含的内容,这是我的解决方案:

  

打印出template块中的变量属于您的组件

然后,您可以重新格式化输出的结构以观察细节。

例如:

<template>
  <div>
    {{ pigeons }}
  <div>
</template>

另一种方式,如果您想在console中看到它,则应将console.log置于mounted阶段。 因为在created阶段,this.pigeons仍然为空。 参考:https://vuejs.org/v2/guide/instance.html#Lifecycle-Diagram

希望这会有所帮助。

答案 10 :(得分:0)

可能的最佳方法

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

obj2 = Object.assign([], obj)

它是由Evan You本人here带来的,还有更多信息

答案 11 :(得分:-1)

我有完全相同的问题,但我解决了:

只需将app.js外部链接移动到head标签。