TypeError: Cannot set property ‘type’ of undefined报错问题
<script>
export default {
data () {
return {
query: '',
// 表格数据
// username: "admin"
// email: "[email protected]"
// mobile: "12345678"
// create_time: 1486720211
// mg_state: true
// id: 500
// role_name: "主管"
userlist: [],
total: -1,
pagenum: 1,
pagesize: 2
}
},
created () {
this.getList()
},
methods: {
async getList () {
// query查询参数可以为空
// pagenum当前页码不能为空
// pagesize每页显示条数不能为空
// 需要授权的 API ,必须在请求头中使用 Authorization 字段提供 token 令牌
const AUTH_TOKEN = localStorage.getItem('token')
this.$http.defaults.headers.common['Authorization'] = AUTH_TOKEN
const res = await this.$http.get(`users?query=${
this.query}&pagenum=${
this.pagenum}&pagesize=${
this.pagesize}`)
console.log(res)
const {
data: {
status,
msg
},
data: {
users,
total
}
} = res.data
if (status === 200) {
// 给表格数据赋值
this.userlist = users
// 给total赋值
this.total = total
// 提示
this.$message.success(msg)
} else {
this.$message.warning(msg)
}
}
}
}
</script>
错误原因:
const {
data: {
status,
msg
},
data: {
users,
total
}
} = res.data
仔细看代码,发现const里面有两个对象,两个都是data,而后台返回的数据应该是一个data和一个meta。
这里也告诉小伙伴们,敲代码的时候一定要细心