You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Axios获取数据后Vuex无法存入,报Cannot read 'code'错误求助

问题分析

控制台报错Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'code'),同时Vuex仓库中categoryList始终为空,核心原因是API请求函数没有返回Promise对象,导致await reqGetCategoryList()得到的result是undefined,无法读取code属性,进而不会执行commit更新仓库数据。

修复步骤

1. 修正API请求函数

在api/index.js中,reqGetCategoryList函数需要返回Axios请求的Promise:

// 修改前
export const reqGetCategoryList = () => {
requests.get('/product/getBaseCategoryList')}

// 修改后
export const reqGetCategoryList = () => {
  return requests.get('/product/getBaseCategoryList')
}

也可以用更简洁的箭头函数写法:

export const reqGetCategoryList = () => requests.get('/product/getBaseCategoryList')

2. 验证逻辑正确性

修改后,await reqGetCategoryList()会正确拿到接口返回的响应对象,当result.code === 200时,会调用commit将result.data存入Vuex的categoryList,此时仓库数据会更新为预期的17项数组。

问题根源

Axios的get方法返回的是一个Promise对象,但原函数没有将这个Promise返回,所以await等待的是一个undefined,自然无法读取code属性,也就不会触发后续的仓库更新逻辑,导致categoryList始终为空。

内容的提问来源于stack exchange,提问作者Zest Zhang

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 18:05:30