Home > other >  Why the error " Cannot set properties of undefined..." appears in Angular 10?
Why the error " Cannot set properties of undefined..." appears in Angular 10?

Time:11-09

I am working on an application where a user can register, for this the user has to write data such as name, nickname, password, type of identification, etc ...

The model of user:

export class User{
    id: string;
    name: string;
    nickname: string;
    password: string;
    docType: {
        documentTypeId: number;
    }
}

The component .ts:

public insertUser(event: Event): void{
      event.preventDefault();
        
      const u: User = new User();

      u.id = this.form.value.id;
      u.name = this.form.value.name;
      u.nickname = this.form.value.nickname;
      u.password = this.form.value.password;
      u.docType.documentTypeId = Number(this.form.value.typeId); // the error occurs here

      /*send to API, save to DB... */ 
}

So, the error occurs when the value of u.docType.documentTypeId is assigned, I don't know what I'm doing wrong.

CodePudding user response:

Does this work:

u.docType = {
    documentTypeId: Number(this.form.value.typeId);
}

I believe it should!

  • Related