45.中医知识问答管理员端对话信息查看功能bug修复(1)
在大体开发完该功能之后,发现实现上出现了很多bug,本篇文章来记录bug修复的过程:
在界面展示对话的过程中发现展现出来的对话记录不正确且用户ID和创建时间都没有展现出来。
在控制台打印出来的对话是这样的:
经过分析,问题主要是
- 数据渲染问题:前端界面显示的都是"无标题"、"ID为空"等默认值,说明数据没有正确绑定
- 数据结构问题:从控制台输出看,数据可能嵌套层级不符合前端预期
所以对后端和前端进行相应的修改:
修改Controller
@GetMapping
public ResponseEntity<?> getAllConversations() {try {List<Conversation> conversations = adminConversationService.getAllConversations();List<ConversationDTO> dtos = conversations.stream().map(conv -> {ConversationDTO dto = new ConversationDTO();dto.setId(conv.getId());dto.setTitle(conv.getTitle());dto.setCreatedAt(conv.getCreatedAt());if(conv.getUser() != null) {dto.setUserId(conv.getUser().getUserId());dto.setUsername(conv.getUser().getUsername());}dto.setMessageCount(conv.getMessages() != null ? conv.getMessages().size() : 0);return dto;}).collect(Collectors.toList());return ResponseEntity.ok(dtos);} catch (Exception e) {// 错误处理...}
}
4. 前端适配修改
修改前端store中的fetchAllConversations方法:
const fetchAllConversations = async () => {loading.value = true;error.value = null;try {const response = await axios.get('http://localhost:8080/api/admin/conversations');// 处理可能的数据结构变化conversations.value = response.data.map(item => ({id: item.id,title: item.title || '无标题',createdAt: item.createdAt,user: {userId: item.userId || item.user?.userId,username: item.username || item.user?.username || '未知用户'},messages: item.messages || [],messageCount: item.messageCount || 0}));} catch (err) {error.value = err.response?.data?.message || err.response?.data?.error || '获取对话列表失败';console.error('API错误详情:', err.response);} finally {loading.value = false;}
};
关键修改点说明
-
后端增强:
- 添加了异常处理和DTO转换
- 确保关联数据被正确加载
- 返回更结构化的数据
-
前端适配:
- 处理多种可能的数据结构
- 更好的错误信息展示
- 数据映射更健壮
修改完成后,对话基本信息大致可以呈现出来:
此bug修复完成