我已经为CRUD功能开发了一个简单的均值堆栈应用程序。现在我的后端可以正常工作,即将数据发布到MongoDB,但是前端似乎给了我一个问题:
ERROR Type Error: Cannot read property '_id' of undefined
at Object.eval [as updateDirectives] (EmployeeComponent.html:4)
at Object.debugUpdateDirectives [as updateDirectives] (core.js:10846)
at checkAndUpdateView (core.js:10243)
at callViewAction (core.js:10484)
at execComponentViewsAction (core.js:10426)
at checkAndUpdateView (core.js:10249)
at callViewAction (core.js:10484)
at execComponentViewsAction (core.js:10426)
at checkAndUpdateView (core.js:10249)
at callWithDebugContext (core.js:11136)
在以下位置遇到此错误:-
<form #employeeForm="ngForm" (ngSubmit)="onSubmit(employeeForm)">
<input type="hidden" name="_id" #_id="ngModel" [(ngModel)]="employeeService.selectedEmployee._id">
<div class="row">
<div class="input-field col s12">
<input type="text" name="name" #name="ngModel" [(ngModel)]="employeeService.selectedEmployee.name">
<label>Name :
<label class="red-text">*</label>
</label>
答案 0 :(得分:1)
由于您是异步地从api检索数据,因此请使用* ngIf处理空数据,如下所示,
<form *ngIf="employeeService?.selectedEmployee?" #employeeForm="ngForm" (ngSubmit)="onSubmit(employeeForm)">
<input type="hidden" name="_id" #_id="ngModel" [(ngModel)]="employeeService.selectedEmployee._id">
<div class="row">
<div class="input-field col s12">
<input type="text" name="name" #name="ngModel" [(ngModel)]="employeeService.selectedEmployee.name">
<label>Name :
<label class="red-text">*</label>
</label>