1

I've developed a simple mean stack application for CRUD functionalities. Now my back-end is working correctly i.e its posting data to MongoDB but the front-end seems to give me an issue that says:

    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)

This error is encountered at:-

  <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>

1 Answer 1

1

Since you are retriving the data from api asynchronously, handle the null data with an *ngIf check as follows,

 <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>
Sign up to request clarification or add additional context in comments.

1 Comment

thank you for the feed back!. but i've managed to solve my difficulty. altho ur answer was correct i think the problem was that i hadn;t enable cors origin as im using Nodejs for the back-end. so angular could'nt recognise the ids as it was'nt configured with node

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.