TypeError: Cannot read properties of undefined (reading 'data') at Object.po
1.页面加载,接口POST请求,报错:错误1:请求返回空响应 ...index.ts:34【src/config/axios/index.ts】
错误2:POST请求失败: TypeError: Cannot read properties of undefined (reading 'data') at Object.post
post: async <T = any>(option: any) => {
//const res = await request({ method: 'POST', ...option })
//return res.data as unknown as T
try {
const res = await request({ method: 'POST', ...option });
// 添加空值检查
if (!res) {
console.error('<font color="#ff0000">请求返回空响应</font>'); //<font color="#ff0000">错误1</font>
}
return res.data as unknown as T;
} catch (error) {
console.error('POST请求失败:', error);//<font color="#ff0000">错误2</font>
}
},
错误1:Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'list') at getList
上面的错误出处:
const getList = async () => {
loading.value = true
try {
// 工作台获取工作任务分页
const res = await workbenchApi.getWorkbenchList(queryParams.value)
list.value = res.list //错误1
total.value = res.total
// 获取工作任务统计
workbenchList.value = await workbenchApi.getWorkbenchCount()
if(workbenchList.value){
getDictObj('FLOW_BUS_OPT')
}
loading.value = false
} finally {
loading.value = false
}
}
3.上面的接口报错后,页面返回登录页面,进行登录,报错
Uncaught (in promise) Error: No match for
{"name":"Home","params":{}}
登录页面没有跳转,报错提示上面的错误。
怎么解决?
解决办法:
post: async <T = any>(option: any) => {
try {
const res = await request({ method: 'POST', ...option });
const data = res?.data; // 使用可选链操作符安全访问 data
if (data) {
// 处理有效数据
return data;
}else {
console.error('接口返回空数据');
return null; // 或抛出自定义错误
}
} catch (error) {
console.error('POST请求失败:', error);
}
},
有待测试,看看效果。
页:
[1]