From b9cb173eb69782fd2f3b1a8bb67e68ae284bce1f Mon Sep 17 00:00:00 2001 From: AI Clinical Dev Team Date: Fri, 10 Oct 2025 21:33:38 +0800 Subject: [PATCH] fix: resolve double data unwrapping issue in API calls MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Issue: projectApi and agentApi were unwrapping response.data twice: 1. request interceptor returns response.data 2. API functions returned response.data again (undefined) This caused all API calls to fail silently with 'response.success' being undefined. Fixed: - projectApi: Removed redundant .data access - agentApi: Removed redundant .data access All API functions now correctly return the backend response: { success: true, data: [...] } This fixes: - '鑾峰彇椤圭洰鍒楄〃澶辫触' error on page load - '鍒涘缓椤圭洰澶辫触' error when creating projects - Any other API call failures --- frontend/src/api/agentApi.ts | 18 +++---- frontend/src/api/projectApi.ts | 15 ++---- 测试API.bat | 89 ++++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 22 deletions(-) create mode 100644 测试API.bat diff --git a/frontend/src/api/agentApi.ts b/frontend/src/api/agentApi.ts index f2ef1fe4..fc3142b5 100644 --- a/frontend/src/api/agentApi.ts +++ b/frontend/src/api/agentApi.ts @@ -33,32 +33,27 @@ export interface ApiResponse { export const agentApi = { // 获取所有智能体列表 getAll: async (): Promise> => { - const response = await request.get('/agents'); - return response.data; + return await request.get('/agents'); }, // 获取启用的智能体列表 getEnabled: async (): Promise> => { - const response = await request.get('/agents/enabled'); - return response.data; + return await request.get('/agents/enabled'); }, // 获取单个智能体详情 getById: async (id: string): Promise> => { - const response = await request.get(`/agents/${id}`); - return response.data; + return await request.get(`/agents/${id}`); }, // 根据分类获取智能体 getByCategory: async (category: string): Promise> => { - const response = await request.get(`/agents/by-category?category=${encodeURIComponent(category)}`); - return response.data; + return await request.get(`/agents/by-category?category=${encodeURIComponent(category)}`); }, // 获取智能体的系统Prompt getSystemPrompt: async (id: string): Promise> => { - const response = await request.get(`/agents/${id}/system-prompt`); - return response.data; + return await request.get(`/agents/${id}/system-prompt`); }, // 渲染用户Prompt @@ -70,8 +65,7 @@ export const agentApi = { knowledgeBaseContext?: string; } ): Promise> => { - const response = await request.post(`/agents/${id}/render-prompt`, data); - return response.data; + return await request.post(`/agents/${id}/render-prompt`, data); }, }; diff --git a/frontend/src/api/projectApi.ts b/frontend/src/api/projectApi.ts index 4ba284b0..c7d8f676 100644 --- a/frontend/src/api/projectApi.ts +++ b/frontend/src/api/projectApi.ts @@ -23,20 +23,17 @@ export interface UpdateProjectRequest { export const projectApi = { // 获取项目列表 getProjects: async (): Promise> => { - const response = await request.get('/projects'); - return response.data; + return await request.get('/projects'); }, // 获取单个项目详情 getProjectById: async (id: string): Promise> => { - const response = await request.get(`/projects/${id}`); - return response.data; + return await request.get(`/projects/${id}`); }, // 创建项目 createProject: async (data: CreateProjectRequest): Promise> => { - const response = await request.post('/projects', data); - return response.data; + return await request.post('/projects', data); }, // 更新项目 @@ -44,14 +41,12 @@ export const projectApi = { id: string, data: UpdateProjectRequest ): Promise> => { - const response = await request.put(`/projects/${id}`, data); - return response.data; + return await request.put(`/projects/${id}`, data); }, // 删除项目 deleteProject: async (id: string): Promise => { - const response = await request.delete(`/projects/${id}`); - return response.data; + return await request.delete(`/projects/${id}`); }, }; diff --git a/测试API.bat b/测试API.bat new file mode 100644 index 00000000..6f94bbd8 --- /dev/null +++ b/测试API.bat @@ -0,0 +1,89 @@ +@echo off +chcp 65001 >nul +echo ======================================== +echo API测试工具 +echo ======================================== +echo. + +echo [测试1] 后端健康检查 +echo URL: http://localhost:3001/health +curl -s http://localhost:3001/health +if errorlevel 1 ( + echo. + echo ❌ 后端服务未响应 + echo 解决方案: cd backend ^&^& npm run dev + goto :end +) else ( + echo. + echo ✅ 后端服务正常 +) +echo. +echo ---------------------------------------- +echo. + +echo [测试2] 获取项目列表 +echo URL: http://localhost:3001/api/v1/projects +curl -s http://localhost:3001/api/v1/projects +if errorlevel 1 ( + echo. + echo ❌ 获取项目列表失败 +) else ( + echo. + echo ✅ 获取项目列表成功 +) +echo. +echo ---------------------------------------- +echo. + +echo [测试3] 获取智能体列表 +echo URL: http://localhost:3001/api/v1/agents +curl -s http://localhost:3001/api/v1/agents +if errorlevel 1 ( + echo. + echo ❌ 获取智能体列表失败 +) else ( + echo. + echo ✅ 获取智能体列表成功 +) +echo. +echo ---------------------------------------- +echo. + +echo [测试4] 创建测试项目 +echo URL: http://localhost:3001/api/v1/projects +echo 正在创建项目... +curl -X POST http://localhost:3001/api/v1/projects -H "Content-Type: application/json" -d "{\"name\":\"API测试项目\",\"background\":\"这是通过脚本创建的测试项目\",\"researchType\":\"observational\"}" +if errorlevel 1 ( + echo. + echo ❌ 创建项目失败 +) else ( + echo. + echo ✅ 创建项目成功 +) +echo. +echo ---------------------------------------- +echo. + +echo [测试5] 前端服务检查 +echo URL: http://localhost:3000 +curl -s http://localhost:3000 >nul 2>&1 +if errorlevel 1 ( + echo ❌ 前端服务未响应 + echo 解决方案: cd frontend ^&^& npm run dev +) else ( + echo ✅ 前端服务正常 +) +echo. + +:end +echo ======================================== +echo 测试完成 +echo ======================================== +echo. +echo 如果所有测试都通过,但浏览器仍有问题: +echo 1. 按 Ctrl + Shift + R 强制刷新浏览器 +echo 2. 查看浏览器控制台(F12)的错误信息 +echo 3. 查看解决方案-前端获取数据失败.md +echo. +pause +