## 问题描述 当用户创建新会话但未发送任何消息就切换到其他会话时,前端控制台报错: "TypeError: Cannot read properties of null (reading 'map')" 根本原因:Go 后端未初始化的切片序列化为 JSON 时会变成 `null` 而非 `[]`, 前端尝试对 `null` 调用 `.map()` 导致崩溃。 ## 修复方案 采用多层防御策略,同时修复后端和前端: ### 后端修复(确保 API 契约正确) 1. message_pg.go:93 - 将 `var messages []StoredMessage` 改为 `messages := make([]StoredMessage, 0)`,确保空结果序列化为 `[]` 2. conversation.go - 在两个响应路径(PG 查询 + 内存回退)添加防御性 nil 检查 ### 前端防御(多层保护) 1. useSessionList.ts - 在 loadMessages 和 loadSessions 中添加 null 合并操作 `(res.data.messages || [])` 确保即使后端退化也不会崩溃 ## 影响范围 - 所有空会话(新建后未发送消息的对话)现在可以正常切换 - API 响应符合 JSON 最佳实践(数组字段永远是 `[]` 而非 `null`)
React + TypeScript + Vite
This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.
Currently, two official plugins are available:
- @vitejs/plugin-react uses Oxc
- @vitejs/plugin-react-swc uses SWC
React Compiler
The React Compiler is not enabled on this template because of its impact on dev & build performances. To add it, see this documentation.
Expanding the ESLint configuration
If you are developing a production application, we recommend updating the configuration to enable type-aware lint rules:
export default defineConfig([
globalIgnores(['dist']),
{
files: ['**/*.{ts,tsx}'],
extends: [
// Other configs...
// Remove tseslint.configs.recommended and replace with this
tseslint.configs.recommendedTypeChecked,
// Alternatively, use this for stricter rules
tseslint.configs.strictTypeChecked,
// Optionally, add this for stylistic rules
tseslint.configs.stylisticTypeChecked,
// Other configs...
],
languageOptions: {
parserOptions: {
project: ['./tsconfig.node.json', './tsconfig.app.json'],
tsconfigRootDir: import.meta.dirname,
},
// other options...
},
},
])
You can also install eslint-plugin-react-x and eslint-plugin-react-dom for React-specific lint rules:
// eslint.config.js
import reactX from 'eslint-plugin-react-x'
import reactDom from 'eslint-plugin-react-dom'
export default defineConfig([
globalIgnores(['dist']),
{
files: ['**/*.{ts,tsx}'],
extends: [
// Other configs...
// Enable lint rules for React
reactX.configs['recommended-typescript'],
// Enable lint rules for React DOM
reactDom.configs.recommended,
],
languageOptions: {
parserOptions: {
project: ['./tsconfig.node.json', './tsconfig.app.json'],
tsconfigRootDir: import.meta.dirname,
},
// other options...
},
},
])