From 75cabe83393fa628481eb7cd643a50928af28a31 Mon Sep 17 00:00:00 2001 From: "jif2.zhang" Date: Wed, 28 Oct 2026 09:45:43 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E4=BB=BB=E5=8A=A1):=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E5=BD=92=E6=A1=A3/=E6=81=A2=E5=A4=8D=E5=BD=92=E6=A1=A3?= =?UTF-8?q?=E6=8A=A5=20"Cannot=20access=20'a'=20before=20initialization"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 根因: api/task.js 的 archive / unarchive 里写了 `const now = now()`。 同一函数作用域内的 `const now` 会遮蔽模块级的 now 函数, 而初始化表达式的右侧 在求值时该 const 正处于 TDZ, 于是抛 ReferenceError。 源码里报错信息是 "Cannot access 'now' before initialization", 经 vite 压缩后变量被重命名为单字母, 用户端看到的就是 "Cannt access 'a' before i"。 影响: 点「归档」确认后必然失败 (弹错误框); 恢复归档同理。删除/编辑不受影响。 修复: 局部变量改名为 ts, 并补注释避免后人再写回同名。 验证 (真实 Windows 外壳 + CDP 走 UI): - 点任务行 ⋯ → 归档 → 确认: 无异常, 行从列表消失, 任务数减少; - 服务端确认已落库: archived=t / archivedAt / archiveSource=manual; - 测试数据已还原 (archived=f)。 顺带全仓搜索 `const X = X()` 自遮蔽写法, 仅这两处, 无其它同类隐患。 --- src/api/task.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/api/task.js b/src/api/task.js index 70fdbb4..8ff0789 100644 --- a/src/api/task.js +++ b/src/api/task.js @@ -708,14 +708,16 @@ export const taskApi = { archive: async (id) => { const task = await db().getOptional('SELECT * FROM Task WHERE id = ?', [String(id)]) if (!task) throw new Error('任务不存在或无权限') - const now = now() + // 不能写 const now = now(): 同名 const 会遮蔽模块级 now, 右侧求值落入 TDZ, + // 压缩后报 "Cannot access 'a' before initialization" + const ts = now() await db().writeTransaction(async (tx) => { const kids = await tx.getAll('SELECT id FROM Task WHERE parentId = ?', [String(task.id)]) const ids = [String(task.id), ...kids.map((k) => String(k.id))] const ph = ids.map(() => '?').join(',') await tx.execute( `UPDATE Task SET archived = 1, archivedAt = ?, archiveSource = 'manual', updatedAt = ? WHERE id IN (${ph})`, - [now, now, ...ids], + [ts, ts, ...ids], ) }) return { ok: true, count: 1 } @@ -725,14 +727,14 @@ export const taskApi = { unarchive: async (id) => { const task = await db().getOptional('SELECT * FROM Task WHERE id = ?', [String(id)]) if (!task) throw new Error('任务不存在或无权限') - const now = now() + const ts = now() await db().writeTransaction(async (tx) => { const kids = await tx.getAll('SELECT id FROM Task WHERE parentId = ?', [String(task.id)]) const ids = [String(task.id), ...kids.map((k) => String(k.id))] const ph = ids.map(() => '?').join(',') await tx.execute( `UPDATE Task SET archived = 0, archivedAt = NULL, archiveSource = NULL, updatedAt = ? WHERE id IN (${ph})`, - [now, ...ids], + [ts, ...ids], ) }) return { ok: true, count: 1 }