Click here to Skip to main content
15,867,860 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
If data persent in all section of Api so not get any type error if any section is null so give me type error.

I will share you a code

this is html component file.
<div *ngIf="SingleCountryData" class="card">
                <div class="card-body">
                    class="title">Latitude & Longitude:-<p class="card-text">{{SingleCountryData[0].latlng}}</p>
                    class="title">Calling Codes:-<p class="card-text">{{SingleCountryData[0].callingCodes}}</p>
                    class="title">Alt Spellings:-<p class="card-text">{{SingleCountryData[0].altSpellings}}</p>
                    class="title">Demonym:-<p class="card-text">{{SingleCountryData[0].demonym}}</p>
                    class="title">Gini:-<p class="card-text">{{SingleCountryData[0].gini}}</p>
                    class="title">Border:-<p class="card-text">{{SingleCountryData[0].borders}}</p>
                </div>
            </div>
        </div>
        <div class="col-md-6">
            <div class="card">
                <div class="card-body">
                    class="title">
                    <p *ngIf="SingleCountryData" class="card-text">
                        ^__b>Code:-<span><a [routerLink]="['/region/countries',SingleCountryData[0].region, SingleCountryData[0].name, SingleCountryData[0].currencies[0].code]">{{SingleCountryData[0].currencies[0].code}}</a></span><br>
                        Name:-<span>{{SingleCountryData[0].currencies[0].name}}</span><br>
                        Symbol:-<span>{{SingleCountryData[0].currencies[0].symbol}}</span><br>
                    </p>
                    class="title">
                    <p  *ngIf="SingleCountryData" class="card-text">
                        ^__b>iso639_1:-<span> {{SingleCountryData[0].languages[0].iso639_1}}</span><br>
                        iso639_2:-<span> {{SingleCountryData[0].languages[0].iso639_2}}</span><br>
                        Name:-<span> {{SingleCountryData[0].languages[0].name}}</span><br>
                        NativeName:-<span> {{SingleCountryData[0].languages[0].nativeName}}</span><br>
                        <br>
                        iso639_1:-<span>{{SingleCountryData[0].languages[1].iso639_1}}</span><br>
                        iso639_2:-<span>{{SingleCountryData[0].languages[1].iso639_2}}</span><br>
                        Name:-<span>{{SingleCountryData[0].languages[1].name}}</span><br>
                        NativeName:-<span>{{SingleCountryData[0].languages[1].nativeName}}</span>
                    </p>
                    class="title">
                    <p *ngIf="SingleCountryData" class="card-text">
                        <span>^__b>de:-{{SingleCountryData[0].translations.de}}</span><br>
                        <span>es:- {{SingleCountryData[0].translations.es}}</span><br>
                        <span>fr:- {{SingleCountryData[0].translations.fr}}</span><br>
                        <span>ja:- {{SingleCountryData[0].translations.ja}}</span><br>
                        <span>it:- {{SingleCountryData[0].translations.it}}</span><br>
                        <span>br:- {{SingleCountryData[0].translations.br}}</span><br>
                        <span>pt:- {{SingleCountryData[0].translations.pt}}</span><br>
                        <span>nl:- {{SingleCountryData[0].translations.nl}}</span><br>
                        <span>hr:- {{SingleCountryData[0].translations.hr}}</span><br>
                        <span>fa:- {{SingleCountryData[0].translations.fa}}</span><br>
                    </p>
                    class="title">
                    <p *ngIf="SingleCountryData" class="card-text">
                        <span>^__b>Acronym:-{{SingleCountryData[0].regionalBlocs[0].acronym}}</span><br>
                        <span>Name:-{{SingleCountryData[0].regionalBlocs[0].name}}</span><br>
                        <span>Other Acronyms:-{{SingleCountryData[0].regionalBlocs[0].otherAcronyms}}</span><br>
                        <span>Other Names:-{{SingleCountryData[0].regionalBlocs[0].otherNames}}</span>
                    </p>
                    class="title">Cioc:- 
                    <p *ngIf="SingleCountryData" class="card-text">{{SingleCountryData[0].cioc}}</p>
                </div>
            </div>
        </div>
    </div>
</div>


this is ts file..
import { HttpClient } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { CountryHttpService } from '../country-http.service';

@Component({
  selector: 'app-single-country-view',
  templateUrl: './single-country-view.component.html',
  styleUrls: ['./single-country-view.component.css']
})
export class SingleCountryViewComponent implements OnInit {
  SingleCountryData: any;

  constructor(private httpservice: CountryHttpService, private _route: ActivatedRoute, private router: Router, private http: HttpClient) { }

  ngOnInit() {
    let cName = this._route.snapshot.paramMap.get('name');
    console.log(cName);

    this.httpservice.getSingleCountry(cName).subscribe(
      (result)=>{
            this.SingleCountryData = result;
            console.log("Get Single Country Data",this.SingleCountryData);
    },
    error => {
      console.log("some error occured");
      console.log(error.errorMessage)
    })
  }

}


I shared a snapsot of output screen

Screenshot (194).png - Google Drive[^]

What I have tried:

I tried to show all data no come any error if data not present in api that show null at there blank space..
Posted
Updated 17-Nov-20 21:14pm
Comments
Richard MacCutchan 21-Oct-20 5:12am    
Then you need to check what data you get. Is it valid or is it null?
Tarun Surana 21-Oct-20 5:15am    
how to check data is valid or null
Richard MacCutchan 21-Oct-20 7:16am    
What data? You really need to explain where in your code this problem occurs and which item is null, and under what conditions. We cannot run your code to find out.
Tarun Surana 21-Oct-20 7:18am    
I have solve this query so dont worry that problem solution get from stackoverflow.
Tarun Surana 21-Oct-20 7:19am    
I have a another question and also i already posted on your website.

1 solution

For those who are facing an issue similar to this, the reason that the bindings are failing here is because there is an assumption that there is actually a value at element 0 of the array. If the array is null then obviously there can't be an element 0. The way to work around this is to add the check for a null element in here like this
JavaScript
Latitude & Longitude:-{{SingleCountryData && SingleCountryData[0].latlng}}
An alternative would be to wrap the text portion inside something like a span and use *ngIf to control its evaluation.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900