yikart/AiToEarn · error · NotFoundException

Task not found

Error message

Task not found

What it means

NestJS HttpException (404) thrown by AdminTaskService.findOne when Mongoose findById returns no Task document. It is the canonical lookup helper, so any caller resolving a task by id surfaces this error when the id does not exist.

Source

Thrown at project/aitoearn-electron/server/src/modules/task/adminTask.service.ts:84

    if (keyword) filter.title = new RegExp(keyword, 'i');
    if (status !== undefined) filter.status = status;

    return paginateModel(
      this.taskModel,
      {
        page,
        pageSize,
      },
      filter,
      undefined,
      { _id: -1 },
    );
  }

  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 update(id: string, data: UpdateTaskDto): Promise<Task> {
    const updatedTask = await this.taskModel.findByIdAndUpdate(id, data).exec();
    if (!updatedTask) throw new NotFoundException('Task not found');
    return updatedTask;
  }

  /**
   * 更新任务状态
   * @param task
   * @param status
   * @returns
   */
  async updateStatus(task: Task, status: TaskStatus): Promise<Task> {
    if (status === TaskStatus.ACTIVE && task.type === TaskType.ARTICLE) {

View on GitHub (pinned to d3aa8bea5b)

Solutions

  1. Confirm the task id exists (query the tasks collection or admin list endpoint).
  2. Validate the id is a 24-hex-char Mongo ObjectId before calling the API.
  3. Refresh the client's task list after deletions so stale ids are not reused.

Example fix

// caller-side before
const task = await taskService.findOne(id);
// after
import { isValidObjectId } from 'mongoose';
if (!isValidObjectId(id)) throw new BadRequestException('Invalid task id');
const task = await taskService.findOne(id);
Defensive patterns

Strategy: try-catch

Validate before calling

import { isValidObjectId } from 'mongoose';
if (!isValidObjectId(id)) throw new BadRequestException('Invalid task id');

Type guard

function isTask(t: Task | null | undefined): t is Task {
  return !!t && typeof t._id === 'string';
}

Try / catch

try {
  const task = await api.getTask(id);
} catch (e) {
  if (e.response?.status === 404) {
    return { task: null, reason: 'not-found' };
  }
  throw e;
}

Prevention

When it happens

Trigger: Any admin task endpoint that resolves a task by path :id where the id is nonexistent, deleted, or malformed (invalid ObjectId yields null from findById).

Common situations: Task deleted by another admin while a client still holds its id; id copied from the wrong environment; client sending a truncated or non-ObjectId string.

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/1ddd990fa444be3f. Report an issue: GitHub.