fix(任务): 修复归档/恢复归档报 "Cannot access 'a' before initialization"

根因: 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()` 自遮蔽写法, 仅这两处, 无其它同类隐患。
This commit is contained in:
jif2.zhang
2026-10-28 09:45:43 +08:00
parent 58879024a2
commit 75cabe8339
+6 -4
View File
@@ -708,14 +708,16 @@ export const taskApi = {
archive: async (id) => { archive: async (id) => {
const task = await db().getOptional('SELECT * FROM Task WHERE id = ?', [String(id)]) const task = await db().getOptional('SELECT * FROM Task WHERE id = ?', [String(id)])
if (!task) throw new Error('任务不存在或无权限') 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) => { await db().writeTransaction(async (tx) => {
const kids = await tx.getAll('SELECT id FROM Task WHERE parentId = ?', [String(task.id)]) 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 ids = [String(task.id), ...kids.map((k) => String(k.id))]
const ph = ids.map(() => '?').join(',') const ph = ids.map(() => '?').join(',')
await tx.execute( await tx.execute(
`UPDATE Task SET archived = 1, archivedAt = ?, archiveSource = 'manual', updatedAt = ? WHERE id IN (${ph})`, `UPDATE Task SET archived = 1, archivedAt = ?, archiveSource = 'manual', updatedAt = ? WHERE id IN (${ph})`,
[now, now, ...ids], [ts, ts, ...ids],
) )
}) })
return { ok: true, count: 1 } return { ok: true, count: 1 }
@@ -725,14 +727,14 @@ export const taskApi = {
unarchive: async (id) => { unarchive: async (id) => {
const task = await db().getOptional('SELECT * FROM Task WHERE id = ?', [String(id)]) const task = await db().getOptional('SELECT * FROM Task WHERE id = ?', [String(id)])
if (!task) throw new Error('任务不存在或无权限') if (!task) throw new Error('任务不存在或无权限')
const now = now() const ts = now()
await db().writeTransaction(async (tx) => { await db().writeTransaction(async (tx) => {
const kids = await tx.getAll('SELECT id FROM Task WHERE parentId = ?', [String(task.id)]) 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 ids = [String(task.id), ...kids.map((k) => String(k.id))]
const ph = ids.map(() => '?').join(',') const ph = ids.map(() => '?').join(',')
await tx.execute( await tx.execute(
`UPDATE Task SET archived = 0, archivedAt = NULL, archiveSource = NULL, updatedAt = ? WHERE id IN (${ph})`, `UPDATE Task SET archived = 0, archivedAt = NULL, archiveSource = NULL, updatedAt = ? WHERE id IN (${ph})`,
[now, ...ids], [ts, ...ids],
) )
}) })
return { ok: true, count: 1 } return { ok: true, count: 1 }