我已经为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>
最佳答案
由于您是从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>
关于node.js - TypeError:无法读取Angular4中未定义的属性“_id”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51344790/