【发布时间】:2018-05-07 13:14:16
【问题描述】:
您好朋友,当我使用 vue.js 和 Laravel 计算通知长度时,我厌倦了收到此错误,它显示错误,未捕获(承诺)类型错误:无法读取未定义的属性 'forEach' 请帮助我。这是我的代码。
store.js
import Vuex from 'vuex'
import Vue from 'vue'
Vue.use(Vuex)
export const store = new Vuex.Store({
state: {
nots: []
},
getters: {
all_nots(state) {
return state.nots
},
all_nots_count(state) {
return state.nots.length
},
},
mutations: {
add_not(state, not) {
state.nots.push(not)
},
}
})
UnreadNots.vue
<template>
<li>
<a href="/notifications">
Unread notifications
<span class="badge">{{ all_nots_count }}</span>
</a>
</li>
</template>
<script>
export default {
mounted() {
this.get_unread()
},
methods: {
get_unread() {
this.$http.get('/get_unread')
.then( (nots) => {
nots.body.forEach( (not) => {
this.$store.commit('add_not', not)
})
})
}
},
computed: {
all_nots_count() {
return this.$store.getters.all_nots_count
}
}
}
</script>
web.php
Route::get('get_unread', function(){
return Auth::user()->unreadNotification;
});
【问题讨论】:
-
只有
console.log(nots),很明显它没有body -
我得到了空白数据
-
是的,那告诉你什么?您的服务器可能没有发送任何内容。检查浏览器控制台中的网络选项卡并查看调用返回的内容
-
请截图
/get_unread返回的内容。您可以在浏览器或邮递员中打开它。