-2

I developed the auto-fill form, based on ng-select data it automatically fills the data. But the only issue I am facing is when I am trying to delete the selected data it's throwing me a Cannot read property 'pincode' of null error. Please find and try to resolve this bug. I am sharing my code below. Thanks in advance.

  1. register-component.html
<div class="wrapper">
  <div class="main_content">
    <div class="header">
      <strong><b>Tell us more about you</b></strong>
    </div>
    <div class="info">
      <h3>Basic Details</h3>

      <form [formGroup]="form">
        <div class="form-row">
          <div class="col-md-4 mb-3">
            <label for="validationDefault01"> Name*</label>

            <input
              type="text"
              class="form-control no-border"
              id="validationDefault01"
              placeholder="Enter your name"
              required
            />
          </div>
          <div class="col-md-4 mb-3">
            <label for="validationDefault02">Email*</label>
            <input
              type="email"
              class="form-control"
              id="validationDefault02"
              placeholder="example@domain.com"
              required
            />
          </div>
          <div class="col-md-4 mb-3">
            <label for="validationDefault06">Pin Code*</label>
         
            <ng-select
              class="custom"
              [items]="observeData"
              name="somthing"
              bindLabel="pincode"
              autofocus
              (ngModelChange)="onPincodeSelected($event)"
              [(ngModel)]="PinSelected"
              [ngModelOptions]="{ standalone: true }"
              highlightColor="#9575cd"
              highlightTextColor="#fff"
            >
            </ng-select>
            
          </div>
          <div class="col-md-4 mb-3">
            <label for="validationDefault03">Village*</label>
            <input
              type="text"
              class="form-control"
              id="validationDefault03"
              placeholder="Your Village"
              required
              [value]="PinSelected.village" // Here I am auto-filling the village name
              readonly
            />
          </div>
          <div class="col-md-4 mb-3">
            <label for="validationDefault03">Taluka*</label>
            <input
              type="text"
              class="form-control"
              id="validationDefault03"
              placeholder="Your Taluka"
              required
              [value]="PinSelected.taluka"   // Here I am auto-filling the taluka name
              readonly
            />
          </div>
          <div class="col-md-4 mb-3">
            <label for="validationDefault04">District*</label>
            <input
              type="text"
              class="form-control"
              id="validationDefault04"
              placeholder="Your state"
              required
              [value]="PinSelected.district" // Here I am auto-filling the district name
              readonly
            />
          </div>
          <div class="col-md-4 mb-3">
            <label for="validationDefault05">State*</label>
            <input
              type="text"
              class="form-control"
              id="validationDefault05"
              placeholder="Your District"
              required
              [value]="PinSelected.state" // Here I am auto-filling the State name 
              readonly
            />
          </div>
          
        </div>
        <button class="button"><span>Continue </span></button>
      </form>

     

      <br />
    </div>
  </div>
</div>

  1. register-page.component.ts
import { Component, OnInit } from '@angular/core';
import { data } from 'jquery';
import { Pincodes } from 'src/app/Model/Pincodes';
import { MetadataServiceService } from 'src/app/shared/services/metadata-service.service';
import { Observable, of } from 'rxjs';

import 'rxjs/add/operator/filter';
import { FormGroup } from '@angular/forms';




@Component({
  selector: 'app-register-page',
  templateUrl: './register-page.component.html',
  styleUrls: ['./register-page.component.css']
})

export class RegisterPageComponent implements OnInit {

  
 observeData :Observable< Pincodes[]>  

  modifiedText: any;
  PinSelected : any = {}
 searchValue : string
 form: FormGroup;
 
 constructor(private _metadataService: MetadataServiceService) { 
   
    
    
  }

 ngOnInit(){
    
   this._metadataService.GetPincodes()
   .subscribe(res=> {
      this.observeData = res.data;
    });


  
  

  


onPincodeSelected(val : Pincodes){
    console.log("value" + val);
    this.customFunction(val)

    
  }
  customFunction(val : Pincodes){
    this.modifiedText = " Pin : " + val.pincode + "\n" + "  District : " + val.village + "\n" + "  Taluka : " + val.taluka
      console.log("modifiedText" + this.modifiedText);
      console.log("Pincode Selected" + this.PinSelected);
      console.log("observeData Selected" + this.observeData);
      
      
      
      
      
  }
  
 
  

}

enter image description here

when I am clicking that cross to enter new data. Its showing me those two errors

messi
  • 11
  • 1
  • 4

2 Answers2

0

This erros is because you don't have a property village definec in PinSelected component, you could fix it changing line 60 of your HTML from PinSelected.village to PinSelected?.village

0

You have 3-4 div tags where you are populating data based on a previous div.

You can enclose the 3-4 div tags into another div tag or ng-container tag and add a condition like this.

<ng-container *ngIf="pinSelected">
  <div1></div1>
  <div2></div2>
  <div3></div3>
</ng-container>
Srikar Phani Kumar M
  • 1,069
  • 1
  • 3
  • 8