Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to read property 'id' when dragging tasks. #4

Closed
1plam opened this issue Jul 31, 2023 · 1 comment
Closed

Unable to read property 'id' when dragging tasks. #4

1plam opened this issue Jul 31, 2023 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@1plam
Copy link
Owner

1plam commented Jul 31, 2023

Issue Description:

When attempting to drag tasks within the project board, users encounter a TypeError: "Cannot read properties of undefined (reading 'id')" error. This issue arises due to the 'id' property in the 'user' object being undefined. The task's data is derived from the UpdateBoardTaskRequest interface, which includes both 'user' and 'userId' properties.

Steps to Reproduce:

  1. Navigate to the project board.
  2. Attempt to drag a task from one column to another.

Expected Behavior:
The task should be draggable without any errors, and the 'id' property in the 'user' object should be accessible.

Actual Behavior:
Dragging tasks results in a TypeError due to the 'id' property in the 'user' object being undefined.

Proposed Solution:

To resolve this issue, we need to implement additional checks in the drop method of the project board component. Specifically, we need to ensure that the 'user' object is defined and has a valid 'id' property before accessing it. This will allow us to set the 'userId' based on the 'user.id' if available and avoid accessing undefined properties.

Additional Information:
Error Code: Uncaught (in promise): TypeError: Cannot read properties of undefined (reading 'id')

Issue occurrance:

async drop(event: CdkDragDrop<BoardTask[]>) {
  if (event.previousContainer === event.container) {
    moveItemInArray(event.container.data, event.previousIndex, event.currentIndex);
  } else {
    transferArrayItem (
        event.previousContainer.data,
        event.container.data,
        event.previousIndex,
        event.currentIndex
    );

    const droppedTask: UpdateBoardTaskRequest = event.item.data;
    droppedTask.status = this.status;

    const indexToReplace = this.filteredBoardTasks.findIndex(task => task.id === droppedTask.id);
    this.filteredBoardTasks[indexToReplace] = {...droppedTask, userId: droppedTask.user.id};

    await this.boardTaskService.updateBoardTask(droppedTask);
  }
}

Impact:

This issue affects users trying to manage tasks within the project board. The error prevents smooth task movement and may cause confusion and frustration.

@1plam 1plam added the bug Something isn't working label Jul 31, 2023
@1plam 1plam self-assigned this Jul 31, 2023
@1plam 1plam pinned this issue Jul 31, 2023
1plam added a commit that referenced this issue Jul 31, 2023
This fixes issue #4 occurring an error when a task was moved between board columns.
@1plam
Copy link
Owner Author

1plam commented Jul 31, 2023

Update:

The error was occurring due to the 'id' property in the 'user' object being undefined, which is part of the 'UpdateBoardTaskRequest' interface.

To resolve this issue, I have made the following changes:

In the 'drop' method of the project board component, I implemented additional checks to ensure that the 'user' object is defined and contains a valid 'id' property before accessing it. This prevents the TypeError from occurring during task movement.

async drop(event: CdkDragDrop<BoardTask[]>) {
  if (event.previousContainer === event.container) {
    moveItemInArray(event.container.data, event.previousIndex, event.currentIndex);
  } else {
    transferArrayItem(
      event.previousContainer.data,
      event.container.data,
      event.previousIndex,
      event.currentIndex
    );

    const droppedTask: UpdateBoardTaskRequest = {
      ...event.item.data,
      status: this.status,
      user: event.item.data.userId !== null ? await this.userService.getUserById(event.item.data.userId) : null
    };

    this.mapAndFilterTasks(droppedTask);
    await this.boardTaskService.updateBoardTask(droppedTask);
  }
}

In the updated code, I ensured that the 'userId' property is set based on the 'user.id' if available. This way, the 'UpdateBoardTaskRequest' object contains a valid 'user' object, and the 'id' property is accessible without any issues.

However, I must note that while this fix resolves the immediate issue, it is not considered a best practice to handle user objects in this manner. Directly setting 'userId' based on 'user.id' within the 'UpdateBoardTaskRequest' object can lead to potential issues and reduced code maintainability.

@1plam 1plam closed this as completed Jul 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant