【问题标题】:having vuefire+firebase binding collections error出现 vuefire+firebase 绑定集合错误
【发布时间】:2019-01-17 06:32:13
【问题描述】:

我正在使用 VueFire 和 Firebase 构建网络应用程序。

我的实时数据库中有两个集合:

cities:{
    0: {
        name: Berlin,
        countryId: 0
    },
    1: {
        name: Paris,
        countryId: 1
    },
    ...
},
countries:{
    0: {
        name: Germany,
    },
    1: {
        name: France,
    },
    ...
},

在 Vue 组件中,我试图获取(城市名称 + 国家名称)的列表:

<div v-for="(city, indx) in cities" :key="indx">
    <div>{{city.name}}, {{countries[city.countryId].name}}</div>
</div>

export default {
    firebase: {
      cities: db.ref('cities'),
      countries: db.ref('countries'),
    }
}

出现此错误:

[Vue warn]: Error in render: "TypeError: Cannot read property 
'countryId' of undefined"

如果只有 {{city.name}} - 好的。

怎么了?如何以正确的方式绑定这 2 个集合以获得我想要的?

【问题讨论】:

  • 而这个 {{cities[0].countryId}} 也会导致同样的错误

标签: firebase firebase-realtime-database vue.js vuefire


【解决方案1】:

由于 firebase 需要一些时间来获取数据,因此首先加载 DOM。

你可以试试这样。

firebase: {
  citiesFromdb: db.ref('cities'),
  countriesFromdb: db.ref('countries'),
}
computed: {
    cities() {
        return this.citiesFromdb;
    },
    countries(){
      return this.countriesFromdb;
    }
}

【讨论】:

  • 我已经尝试过这个,使用计算(以及不同的钩子),但不幸的是它没有帮助解决问题
  • 是否可以做console.log(countries),否则会报错
  • console.log(this.countries) - (5) [{...}, {...}, {...}, {...}, {...}, ob:观察者] - 没有错误,所有真实数据都在数组和对象中
  • 这意味着您的国家/地区集合是一个数组,而不是您在问题中显示的那种数据(对象)?
  • оh,我在数据库描述中犯了一个错误。国家是一个数组。
猜你喜欢
  • 2011-12-15
  • 1970-01-01
  • 2018-01-19
  • 2020-06-13
  • 1970-01-01
  • 1970-01-01
  • 2021-06-23
  • 2017-07-03
  • 2014-03-24
相关资源
最近更新 更多