From a0611a3a135f474990599dfe7f3b429b13c9aa44 Mon Sep 17 00:00:00 2001 From: Wenyan Date: Tue, 25 Nov 2025 16:38:07 +0800 Subject: [PATCH] =?UTF-8?q?fix(evaluation):=20=E4=BF=AE=E5=A4=8D=20PostgRE?= =?UTF-8?q?ST=20endpoint=20=E6=A0=BC=E5=BC=8F=E9=94=99=E8=AF=AF=E5=AF=BC?= =?UTF-8?q?=E8=87=B4=20404?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## 问题描述 更新或获取评查点时出现 404 错误: - GET http://172.16.0.55:8073/evaluation_points/736 (404 Not Found) - 用户报告"删除失败" ## 根本原因 `getRule` 和 `updateRule` 函数使用了错误的 PostgREST endpoint 格式: - ❌ 错误:`evaluation_points/${id}` (REST风格路径) - ✅ 正确:`evaluation_points?id=eq.${id}` (PostgREST查询参数) PostgREST **不支持** `/resource/{id}` 风格的路径参数, 必须使用查询参数格式:`/resource?id=eq.{id}` ## 修复内容 ### getRule 函数 (line 416-470) - 更新endpoint从 `evaluation_points/${id}` 为 `'evaluation_points'` - 添加 `filter: { 'id': \`eq.${id}\` }` 到 PostgrestParams - 更新响应数据处理逻辑,支持数组和包装对象两种格式 - 改进错误处理,返回404而不是500 ### updateRule 函数 (line 728-754) - 更新endpoint从 `evaluation_points/${id}` 为 `'evaluation_points'` - 添加filter参数:`{ id: parseInt(id) }` 到 `postgrestPut` 第三个参数 - 更新响应数据处理逻辑,支持数组和包装对象两种格式 - 修复数据映射,使用 `updatedRule` 而不是 `response.data.data` ## 技术说明 PostgREST 标准查询语法: - 单条记录查询:`GET /table?id=eq.123` - 更新操作:`PATCH /table?id=eq.123` + body - 不支持:`GET /table/123` 或 `PATCH /table/123` ## 测试建议 1. 刷新浏览器页面(Ctrl+F5) 2. 尝试编辑评查点并保存 3. 检查控制台不再出现404错误 4. 验证更新成功并正确返回数据 --- app/api/evaluation_points/rules.ts | 56 ++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 15 deletions(-) diff --git a/app/api/evaluation_points/rules.ts b/app/api/evaluation_points/rules.ts index 19c21e0..a137b45 100644 --- a/app/api/evaluation_points/rules.ts +++ b/app/api/evaluation_points/rules.ts @@ -417,6 +417,8 @@ export async function getRule(id: string, token?: string): Promise<{data: Rule; try { // 使用postgrestGet获取单个评查点数据 const postgrestParams: PostgrestParams = { + // 使用PostgREST查询参数语法 + filter: { 'id': `eq.${id}` }, // 使用PostgREST资源嵌入语法获取关联数据 select: ` id, @@ -440,21 +442,32 @@ export async function getRule(id: string, token?: string): Promise<{data: Rule; `, token }; - - // 获取评查点详情 - const response = await postgrestGet<{code: number; msg: string; data: ApiRule}>(`evaluation_points/${id}`, postgrestParams); + + // 获取评查点详情 - 使用正确的PostgREST格式 + const response = await postgrestGet<{code: number; msg: string; data: ApiRule} | ApiRule[]>('evaluation_points', postgrestParams); // 检查是否有错误响应 if (response.error) { return { error: response.error, status: response.status }; } - - // 确保响应数据存在且符合预期格式 - if (!response.data || !response.data.data) { - return { error: '接口返回数据格式不正确', status: 500 }; + + // 处理响应数据(PostgREST可能返回数组或包装对象) + let apiRule: ApiRule | null = null; + + if (response.data) { + // 如果是数组格式(PostgREST标准响应) + if (Array.isArray(response.data)) { + apiRule = response.data.length > 0 ? response.data[0] : null; + } + // 如果是包装对象格式 + else if ('data' in response.data && response.data.data) { + apiRule = response.data.data as ApiRule; + } + } + + if (!apiRule) { + return { error: '评查点不存在', status: 404 }; } - - const apiRule = response.data.data; // 获取分组信息 try { @@ -711,21 +724,34 @@ export async function updateRule(id: string, ruleData: Partial(`evaluation_points/${id}`, apiRuleData, undefined, token); + // 使用postgrestPut更新评查点 - 使用正确的PostgREST格式 + const response = await postgrestPut<{code: number; msg: string; data: ApiRule} | ApiRule[], typeof apiRuleData>('evaluation_points', apiRuleData, { id: parseInt(id) }, token); // 检查是否有错误响应 if (response.error) { return { error: response.error, status: response.status }; } - // 确保响应数据存在且符合预期格式 - if (!response.data || !response.data.data) { - return { error: '接口返回数据格式不正确', status: 500 }; + // 处理响应数据(PostgREST可能返回数组或包装对象) + let updatedRule: ApiRule | null = null; + + if (response.data) { + // 如果是数组格式(PostgREST标准响应) + if (Array.isArray(response.data)) { + updatedRule = response.data.length > 0 ? response.data[0] : null; + } + // 如果是包装对象格式 + else if ('data' in response.data && response.data.data) { + updatedRule = response.data.data as ApiRule; + } + } + + if (!updatedRule) { + return { error: '更新成功但无法获取更新后的数据', status: 500 }; } // 将API返回的数据映射到前端模型 - const rule = mapApiRuleToFrontendModel(response.data.data); + const rule = mapApiRuleToFrontendModel(updatedRule); return { data: rule }; } catch (error) {