yikart/AiToEarn · error · NotFoundException
Task not found
Error message
Task not found
What it means
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.
Source
Thrown at project/aitoearn-electron/server/src/modules/task/task.service.ts:110
{ $skip: (page - 1) * pageSize }, // 跳过前面的记录
{ $limit: pageSize }, // 限制每页的记录数量
]);
const totaP = this.taskModel.countDocuments(filter);
const [items, total] = await Promise.all([listP, totaP]);
return createPaginationObject<Task>({
items,
totalItems: total,
currentPage: page,
limit: pageSize,
});
}
async findOne(id: string): Promise<Task> {
const task = await this.taskModel.findById(id).exec();
if (!task) {
throw new NotFoundException('Task not found');
}
return task;
}
// 统计合计进行中的任务的金额总数
async getTotalAmountOfDoingTasks(userId: string): Promise<number> {
const tasks = await this.userTaskModel.find({
status: UserTaskStatus.APPROVED,
userId: new ObjectId(userId),
});
let totalAmount = 0;
for (const task of tasks) totalAmount += task.reward;
return totalAmount;
}
// 根据ID获取素材View on GitHub (pinned to d3aa8bea5b)
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
Example fix
// before const task = await taskService.findOne(id) // throws 404 // after const task = await taskModel.findById(id) if (!task) return null // or custom handling
Defensive patterns
Strategy: try-catch
Validate before calling
const exists = await taskModel.exists({ _id: id })
if (!exists) throw new Error(`Task ${id} not found in this environment`) Type guard
function isObjectId(id: unknown): id is string {
return typeof id === 'string' && /^[a-f\d]{24}$/i.test(id)
} Try / catch
try {
const task = await taskService.findOne(id)
} catch (e) {
if (e instanceof NotFoundException && e.message === 'Task not found') {
// treat as missing resource: refresh list or check environment/DB
} else throw e
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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).
Understand the failure class
Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.
Related errors
AI-assisted analysis of yikart/AiToEarn@d3aa8bea5b (2026-08-31).
Data as JSON: /api/errors/3db560bbb8c2542e.
Report an issue: GitHub.