ERROR TypeError: Cannot read property 'Nom' of undefined

ERROR TypeError: Cannot read property 'Nom' of undefined

当我想在 table:

中添加新行时收到此错误消息
ERROR TypeError: Cannot read property 'Nom' of undefined
  at Object.eval [as updateDirectives] (MedecinsComponent.html:43)
  at Object.debugUpdateDirectives [as updateDirectives] (core.js:36043)
  at checkAndUpdateView (core.js:35055)
  at callViewAction (core.js:35421)
  at execEmbeddedViewsAction (core.js:35378)
  at checkAndUpdateView (core.js:35056)
  at callViewAction (core.js:35421)
  at execComponentViewsAction (core.js:35349)
  at checkAndUpdateView (core.js:35062)
  at callViewAction (core.js:35421)

这是我的代码:

component.ts

this.cols = [
  { field: 'User.Nom', header: 'Nom' },
  { field: 'User.Prénom', header: 'Prénom' },
  { field: 'User.Tel', header: 'Téléphone' },
  { field: 'User.Email', header: 'Email' },
  { field: 'User.Pays', header: 'Pays' },
  { field: 'User.Login', header: 'Login' },
  { field: 'User.Password', header: 'Password' },
  { field: 'User.Active', header: 'Active' },
  { field: 'Details', header: 'Détails'}
];

component.html

<p-dialog header="Détails de l'utilisateur" [(visible)]="displayDialog" [responsive]="true" showEffect="fade" [modal]="true" [closable]="true" [width]="600">
    <div class="ui-g ui-fluid" *ngIf="medecin">              
        <div class="ui-g-4"><label for="Nom">Nom</label></div>
        <div class="ui-g-8"><input pInputText id="Nom" [(ngModel)]="medecin.Users.Nom"/></div>
    </div>
</p-dialog>

model.ts

import { User } from './user';

export class Medecin {
  Id: number;
  Users_Id: number;
  Image: ByteLengthChunk;
  Details: string;
  Updated: Date;
  Created: Date;
  Deleted: Date;
  Users: User;
}

那么,是否存在实施问题?我做错了吗?

因为它说 "can't find property Nom in undefined",看起来你没有对象 User 开始。

您必须先初始化并获取"User"实例的值。 所以在写 "this.col = ..." 之前 尝试获取用户实例,然后将用户属性传递给 "col" 对象数组。