【发布时间】:2021-07-20 05:05:29
【问题描述】:
我正在使用 VueJS / Axios 做一个小项目。 我的控制台日志中出现错误:
Error in render: "TypeError: Cannot read property 'first_name' of undefined"
因为我得到并正确显示了 first_name(我在我的页面中看到了结果),所以问题就在控制台中
这是我的代码:
export default {
data(){
return {
collection:{
data: []
}
}
},
created: function(){
getAPI.get('/api/contacts/1/', [{'x':'10'}]).then((response) => {
this.collection.data = response.data
})
}
}
HTML 代码:
<span class="d-block info-content">
{{collection.data.contactInfos.first_name}}
</span>
顺便说一下,我在我的 HTML 中看到了名称,但我只是想知道我的控制台日志错误,因为代码可以完美运行
【问题讨论】:
-
该错误表明
collection.data中没有contactInfos属性。检查属性的拼写和大小写。 -
顺便说一下,我在我的 html 中看到了结果(这意味着拼写是正确的)
-
这可能来自其他地方。或者错误可能来自其他代码。
-
发生这种情况是因为数据加载是异步的,只需在模板中使用可选链接 {{collection.data?.contactInfos?.first_name}}
标签: javascript vue.js