Encountering issue in Angular 11: Attempting to read properties of undefined, resulting in error message "Cannot read '

I am facing an issue with my edit form. I have added a new function saveSeminar() to save the changes made in the userSeminar data. However, when I try to save the changes, it throws an error even though the data is correctly displayed in HTML. Upon clicking save, it shows as undefined.

Below is the snippet from my component.html file:

<tr *ngFor="let item of userSeminar; let i = index">
  <ng-container>
    <td>
      <input type="text" name="SeminarName" class="form-control table-ta-input"
      placeholder="Seminar Name" [(ngModel)]="userSeminar[i].name">
    </td>
    <td class="text-center">
      <a href="#save-seminar" (click)="saveSeminar(i)">
        <i class="fa fa-check">
        </i>
      </a>
    </td>
  </ng-container>
  <ng-template #noEdit>
    <td>
      {{ item.name }}
    </td>
    <td class="text-center">
      <button class="btn tbl-btn-sm" (click)="editSeminar(i)">
        <i class="fa fa-edit">
        </i>
      </button>
      <button class="btn tbl-btn-sm" href="#confirm-modal" data-toggle="modal">
        <i class="fa fa-trash">
        </i>
      </button>
    </td>
  </ng-template>
</tr>

Component.ts

private saveSeminar(index) {
    event.preventDefault();
    console.log(index)
    this.updateUserSeminar(index);
}

private updateUserSeminar(index) {
    this.apiService.update('users/seminar', this.userSeminars[index].id, this.userSeminars[index])
    .subscribe(
    response => {
        // this.userSeminars[index].edit = false;
        // this.getUserSeminar(index.employee_number);
        console.log('Seminar Successfully updated', 'Success');
    });
}

Upon executing the code, the following error pops up:
https://i.sstatic.net/A6LXR.png

Answer №1

When updating the updateUserSeminar function, make sure to substitute this.userSeminars[index].id with this.userSeminar[index].id and this.userSeminars[index] with this.userSeminar[index], as userSeminar is being utilized in the HTML code.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Protected members in Angular 2 component templates using TypeScript

Reflecting on ways to incorporate members in a component that can be accessed from the template but not from a parent component sparked my curiosity. In exploring TypeScript visibility in Angular 2, I encountered discussions about "public" and "private" d ...

Using TypeScript and encountering Error TS2345 when trying to utilize Promise.all() in a dialog component

I have a TypeScript function that I need help with: private async deleteSelectedLuminaires (): Promise<any[]> { return this.dialogService.open({ viewModel: DeleteLuminaire, model: 'Cancel or Ok', lock: false }) .whenClosed((res ...

How to showcase a basic alert dialog using Material Angular

I'm currently utilizing Material Angular (found on Angular Material). However, I have noticed that the examples provided on the website appear to be overly complex. Additionally, most tutorials available online are either outdated or they focus on usi ...

What causes the error message when attempting to assign an argument of type 'any[]' to a parameter expecting type 'never'?

I encountered the following error in this code: Argument of type 'any[]' is not assignable to parameter of type 'never' var markers: []; this.Getlapoints(this.map.getCenter(), 500000).then(data => { for (var key in data) { ...

Information sent from TypeScript frontend to Java backend is converted into a LinkedHashMap

I have a situation where my typescript frontend is communicating with my java backend using REST. Recently, I added a new simple rest endpoint but encountered an issue when trying to cast the sent object properly because the body being sent is a LinkedHash ...

Jest | Testing Tool for Functions with Multiple Parameters

I have developed a new tool that requires 3 parameters: value (string), service (any - an actual GET service), and name (string). Below is the code for the tool: import { serverErrorResponseUtil } from 'util/serverErrorResponseUtil'; import { H ...

"Utilizing provideMockStore in NgRx 8 for setting the state of a specific State Slice

I have been working on testing a smart component for my NgRx implementation, and the test setup looks like this: describe( 'Component', () => { let store: MockStore<State>; beforeEach( async( () => { TestBed.configureTesting ...

Ensuring accurate properties are sent to popup notifications

As a newcomer to a React & ASP.NET project, I am facing a challenge in displaying upload status notifications. The task may seem simple, but I need help in figuring out how to create popup notifications that indicate which files have been successfully uplo ...

Solving "ERROR TypeError: Cannot read property 'username' of undefined" in Angular

I encountered an error while trying to edit a user with Angular and Laravel. The issue arises when passing user data from the front-end to the back-end. Angular Material is being utilized in this project. The code in edit-user-dialog.component.ts: impo ...

Assessing the validity of a boolean condition as either true or false while iterating through a for loop

I am facing an issue that involves converting a boolean value to true or false if a string contains the word "unlimited". Additionally, I am adding a subscription to a set of values and need to use *NgIf to control page rendering based on this boolean. &l ...

Unable to populate an array with a JSON object using Angular framework

Here is the JSON object I have: Object { JP: "JAPAN", PAK: "PAKISTAN", IND: "INDIA", AUS: "AUSTRALIA" } This JSON data was retrieved as a response from an HTTP GET request using HttpClient in Angular. Now, I want to populate this data into the following ...

Mastering the implementation of limit and offset in your GET HTTP request

When making a GET request, I need to include parameters: offset: <number>, limit: <number>, and an optional parameter sortBy that accepts fields separated by commas. What is the best way to define the GET method? getData(limit: number, offset ...

The Order ID field in the Serenity-Platform's Order Details tab is not registering orders

I've been working on replicating the functionality of Orders-Order detail in my own project. https://i.stack.imgur.com/Bt47B.png My custom module is called Contract and Contract Line item, which I'm using to achieve this. https://i.stack.imgur ...

How can I incorporate my custom component into an Angular 12 modal using NGZorro?

I want to incorporate my own component into an nzmodal. I attempted the following: <nz-modal [(nzVisible)]="isVisible" nzTitle="Create relation" (nzOnCancel)="handleCancel()" (nzOnOk)="handleOk()"> & ...

When the loop neglects to evaluate the condition properly and inaccurately filters the data

this.authService.getmonthsandyear(this.id).subscribe(data => { this.authService.startdate(this.id).subscribe(start_date_data => { for (let a of data) { if ( a.year && a.month !== start_date_data[0].month && start_dat ...

Utilizing Lodash in TypeScript to merge various arrays into one cohesive array while executing computations on each individual element

I am working with a Record<string, number[][]> and attempting to perform calculations on these values. Here is an example input: const input1 = { key1: [ [2002, 10], [2003, 50], ], }; const input2 = { key ...

What is the best way to implement a switch case for the value of a property within an object in a TypeScript file?

The object I'm dealing with looks like this: {a: auth?.type === '1' || auth?.type === '2' || auth?.type === '3' ? { reason: // I need to implement a switch case here : un ...

Vercel: Failed to create symbolic link, permission denied

I have my personal repository available at https://github.com/Shrinivassab/Portfolio. I am currently working on developing a portfolio website. However, when I attempt to execute the vercel build command, I encounter the following error: Traced Next.js ser ...

Retrieving JSON information using dynamic routes in Next.js

I am working with a json file that contains content for various pages categorized under "service". In my nextJS project, I utilize dynamic routes with a file named "[serviceId].tsx" for routing. This setup is functioning correctly. However, I am facing an ...

In TypeScript Next.js 14 APP, object literals are limited to declaring existing properties

I encountered an error in my typescript next.js 14 APP. I need assistance resolving this issue, which states: Object literal may only specify known properties, and 'productPackages' does not exist in type '(Without<ProductCreateInput, Pr ...