fix: resolve double data unwrapping issue in API calls
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
This commit is contained in:
@@ -33,32 +33,27 @@ export interface ApiResponse<T = any> {
|
||||
export const agentApi = {
|
||||
// 获取所有智能体列表
|
||||
getAll: async (): Promise<ApiResponse<AgentConfig[]>> => {
|
||||
const response = await request.get('/agents');
|
||||
return response.data;
|
||||
return await request.get('/agents');
|
||||
},
|
||||
|
||||
// 获取启用的智能体列表
|
||||
getEnabled: async (): Promise<ApiResponse<AgentConfig[]>> => {
|
||||
const response = await request.get('/agents/enabled');
|
||||
return response.data;
|
||||
return await request.get('/agents/enabled');
|
||||
},
|
||||
|
||||
// 获取单个智能体详情
|
||||
getById: async (id: string): Promise<ApiResponse<AgentConfig>> => {
|
||||
const response = await request.get(`/agents/${id}`);
|
||||
return response.data;
|
||||
return await request.get(`/agents/${id}`);
|
||||
},
|
||||
|
||||
// 根据分类获取智能体
|
||||
getByCategory: async (category: string): Promise<ApiResponse<AgentConfig[]>> => {
|
||||
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<ApiResponse<{ agentId: string; systemPrompt: string }>> => {
|
||||
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<ApiResponse<{ agentId: string; renderedPrompt: string }>> => {
|
||||
const response = await request.post(`/agents/${id}/render-prompt`, data);
|
||||
return response.data;
|
||||
return await request.post(`/agents/${id}/render-prompt`, data);
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
@@ -23,20 +23,17 @@ export interface UpdateProjectRequest {
|
||||
export const projectApi = {
|
||||
// 获取项目列表
|
||||
getProjects: async (): Promise<ApiResponse<Project[]>> => {
|
||||
const response = await request.get('/projects');
|
||||
return response.data;
|
||||
return await request.get('/projects');
|
||||
},
|
||||
|
||||
// 获取单个项目详情
|
||||
getProjectById: async (id: string): Promise<ApiResponse<Project>> => {
|
||||
const response = await request.get(`/projects/${id}`);
|
||||
return response.data;
|
||||
return await request.get(`/projects/${id}`);
|
||||
},
|
||||
|
||||
// 创建项目
|
||||
createProject: async (data: CreateProjectRequest): Promise<ApiResponse<Project>> => {
|
||||
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<ApiResponse<Project>> => {
|
||||
const response = await request.put(`/projects/${id}`, data);
|
||||
return response.data;
|
||||
return await request.put(`/projects/${id}`, data);
|
||||
},
|
||||
|
||||
// 删除项目
|
||||
deleteProject: async (id: string): Promise<ApiResponse> => {
|
||||
const response = await request.delete(`/projects/${id}`);
|
||||
return response.data;
|
||||
return await request.delete(`/projects/${id}`);
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
89
测试API.bat
Normal file
89
测试API.bat
Normal file
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user