RackM
RackM

Reputation: 449

Error in getting value from json object

I am getting json response from service i wanted to get region_name property value from first block only. Following is my json object.

[
{
    "blogid": 286,
    "post_title": "Family Time 2",
    "post_content": "",
    "post_date": "2017-07-27 06:26:34",
    "imageurl": "/wp-content/Love-market-banner.jpg",
    "region_name": "Asia",
    "cat_name": [
        {
            "term_id": 9,
            "name": "FAMILY TIME",
            "slug": "family-time",               
            "category_description": "this is family time",
            "cat_name": "FAMILY TIME",
            "category_nicename": "family-time",
            "category_parent": 0
        }
    ]
},
{
    "blogid": 285,
    "post_title": "Family Time 1",
    "post_content": "",
    "post_date": "2017-07-27 06:26:09",
    "imageurl": "/wp-content/reykjavik-xlarge.jpg",
    "region_name": "Asia",
    "cat_name": [
        {
            "term_id": 9,
            "name": "FAMILY TIME",
            "slug": "family-time",               
            "category_description": "this is family time",
            "cat_name": "FAMILY TIME",
            "category_nicename": "family-time",
            "category_parent": 0
        }
    ]
}
]

My typescript code is as follows

Blogbyregion(regid: any)
{
    var self = this;

    self.blogapi.BlogsbyRegion(regid).subscribe(
        x => {

            this.Blogbyreg = x;


        });
}

and in html i have used

{{Blogbyreg[0]?.region_name}}

I am getting result but also getting error

ERROR TypeError: Cannot read property '0' of null

Upvotes: 0

Views: 55

Answers (1)

eko
eko

Reputation: 40677

You should either initialize your Blogbyreg field like Blogbyreg = [] or do a null check for it.

Upvotes: 1

Related Questions