无法阅读财产' X'未定义的Angular2

时间:2017-06-05 12:43:21

标签: angular typescript

我是A2和TypeScript的新手。我不明白为什么我的用户在那时为空。我设法通过使用elvis操作员来摆脱错误,但我不知道如何解决它。每个约会都有用户设置,但我不能 明白为什么它是空的。谢谢!

appointment.component.ts

export class AppointmentComponent implements OnInit {appointmentList: 
Object[];

constructor(private appointmentService: AppointmentService) {
    this.getAppointmentList();
}

getAppointmentList() {
    this.appointmentService.getAppointmentList().subscribe(
        res => {
            this.appointmentList = 
JSON.parse(JSON.parse(JSON.stringify(res))._body);
        },
        error => console.log(error)
    )

}   

appointment.component.html

<tbody>
    <tr *ngFor="let appointment of appointmentList">
        <td>{{appointment.id}}</td>
        <td>{{appointment.user?.firstName}}</td>
        <td>{{appointment.date | date: 'MM/dd/yyyy'}}</td>
        <td>{{appointment.hour}}</td>
        <td>{{appointment.description}}</td>
        <td>{{appointment.confirmed}}</td>
        <td [hidden]="appointment.confirmed"><a 
(click)="confirmAppointment(appointment.id)" style="cursor: 
pointer;">Confirm</a></td>

    </tr>
</tbody>

appointment.service.ts

@Injectable()
export class AppointmentService {

constructor (private http:Http){}

getAppointmentList() {
let url = "http://localhost:8181/api/appointment/all";
return this.http.get(url, { withCredentials: true });
} }

错误堆栈跟踪

EXCEPTION: Error in ./AppointmentComponent class 
AppointmentComponent - inline template:17:7 caused by: Cannot read property 
'firstName' of undefined

TypeError: Cannot read property 'firstName' of undefined
at View_AppointmentComponent1.detectChangesInternal 
(/AppModule/AppointmentComponent/component.ngfactory.js:113)
at View_AppointmentComponent1.AppView.detectChanges (view.js:425)
at View_AppointmentComponent1.DebugAppView.detectChanges (view.js:620)
at ViewContainer.detectChangesInNestedViews (view_container.js:67)
at CompiledTemplate.proxyViewClass.View_AppointmentComponent0. 
detectChangesInternal 
(/AppModule/AppointmentComponent/component.ngfactory.js:318)
at CompiledTemplate.proxyViewClass.AppView.detectChanges (view.js:425)
at CompiledTemplate.proxyViewClass.DebugAppView.detectChanges (view.js:620)
at CompiledTemplate.proxyViewClass.AppView.internalDetectChanges 
(view.js:410)
at 
CompiledTemplate.proxyViewClass.View_AppointmentComponent_Host0.
detectChangesInternal (/AppModule/AppointmentComponent/host.ngfactory.js:29)
at CompiledTemplate.proxyViewClass.AppView.detectChanges (view.js:425)
at CompiledTemplate.proxyViewClass.DebugAppView.detectChanges (view.js:620)
at ViewContainer.detectChangesInNestedViews (view_container.js:67)
at CompiledTemplate.proxyViewClass.View_AppComponent0.detectChangesInternal 
(/AppModule/AppComponent/component.ngfactory.js:56)
at CompiledTemplate.proxyViewClass.AppView.detectChanges (view.js:425)
at CompiledTemplate.proxyViewClass.DebugAppView.detectChanges (view.js:620)

更新

@RestController
@EnableAutoConfiguration
@RequestMapping("/api/appointment")
@PreAuthorize("hasRole('ADMIN')")
public class AppointmentResource {

@Autowired
private AppointmentService appointmentService;

@Autowired
private SendEmail serviceSendEmail;

@RequestMapping("/all")
public List<Appointment> findAppointmentList() {
    List<Appointment> appointmentList = appointmentService.findAll();

    return appointmentList;
}}

0 个答案:

没有答案
相关问题