{"record":{"id":"3db560bbb8c2542e","repo":"yikart/AiToEarn","slug":"task-not-found-3db560","errorCode":null,"errorMessage":"Task not found","messagePattern":"Task not found","errorType":"http","errorClass":"NotFoundException","httpStatus":404,"severity":"error","filePath":"project/aitoearn-electron/server/src/modules/task/task.service.ts","lineNumber":110,"sourceCode":"      { $skip: (page - 1) * pageSize }, // 跳过前面的记录\n      { $limit: pageSize }, // 限制每页的记录数量\n    ]);\n\n    const totaP = this.taskModel.countDocuments(filter);\n    const [items, total] = await Promise.all([listP, totaP]);\n\n    return createPaginationObject<Task>({\n      items,\n      totalItems: total,\n      currentPage: page,\n      limit: pageSize,\n    });\n  }\n\n  async findOne(id: string): Promise<Task> {\n    const task = await this.taskModel.findById(id).exec();\n    if (!task) {\n      throw new NotFoundException('Task not found');\n    }\n    return task;\n  }\n\n  // 统计合计进行中的任务的金额总数\n  async getTotalAmountOfDoingTasks(userId: string): Promise<number> {\n    const tasks = await this.userTaskModel.find({\n      status: UserTaskStatus.APPROVED,\n      userId: new ObjectId(userId),\n    });\n\n    let totalAmount = 0;\n\n    for (const task of tasks) totalAmount += task.reward;\n    return totalAmount;\n  }\n\n  // 根据ID获取素材","sourceCodeStart":92,"sourceCodeEnd":128,"githubUrl":"https://github.com/yikart/AiToEarn/blob/d3aa8bea5b146a8675607cf0144d891aad3e9683/project/aitoearn-electron/server/src/modules/task/task.service.ts#L92-L128","documentation":"HTTP 404 NotFoundException 'Task not found' thrown by TaskService.findOne (task.service.ts:110) when taskModel.findById(id) returns null. Any service/controller path that resolves a task by id via findOne gets this guard, distinguishing a missing Task document from other failures.","triggerScenarios":"Calling any endpoint that internally calls taskService.findOne(id) with an id that has no Task document in MongoDB — wrong id, deleted task, or non-ObjectId string.","commonSituations":"Hardcoded or copied ids in scripts/tests; task deleted by admin while a user still holds a link; environment mismatch (dev DB vs prod DB, so the id simply does not exist there).","solutions":["Verify the task id against the task list endpoint for the connected environment","Confirm you are pointed at the intended database (MONGODB_URI) — ids do not exist across environments","Handle the 404 in the client by refreshing the task list","Check that the id is a valid 24-char ObjectId string"],"exampleFix":"// before\nconst task = await taskService.findOne(id) // throws 404\n// after\nconst task = await taskModel.findById(id)\nif (!task) return null // or custom handling","handlingStrategy":"try-catch","validationCode":"const exists = await taskModel.exists({ _id: id })\nif (!exists) throw new Error(`Task ${id} not found in this environment`)","typeGuard":"function isObjectId(id: unknown): id is string {\n  return typeof id === 'string' && /^[a-f\\d]{24}$/i.test(id)\n}","tryCatchPattern":"try {\n  const task = await taskService.findOne(id)\n} catch (e) {\n  if (e instanceof NotFoundException && e.message === 'Task not found') {\n    // treat as missing resource: refresh list or check environment/DB\n  } else throw e\n}","preventionTips":["Copy ids from the same environment you call (dev vs prod DBs differ)","Validate ObjectId format before querying","Handle deletion of tasks gracefully: task links can 404 after admin removal"],"tags":["http-404","mongodb","resource-not-found"],"backgroundTag":"resource-not-found","analyzedSha":"d3aa8bea5b146a8675607cf0144d891aad3e9683","analyzedAt":"2026-08-31T14:19:24.185Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}