0

I'm trying to get data from sampledata.json

Errors:

  1. {{sampledata}} displaying [object Object]
  2. {{sampleDataModel.Title}} ERROR TypeError: Cannot read property 'Title' of undefined

sampledata.json:

  {
    "isSuccessfull": true,
    "Model": [
        {
            "SampleDataModel": [ 
                {
                  "Title": "SampleData 1",
                  "Description": "Description."
                }
            ],

            "SampleDetailModel": [
            {
                "name": "Donald Trump",
                "id": "111",
                "country": "USA"
            }
           ]
        }
      ]
    }

sampledata.services.ts :

    import { Injectable } from '@angular/core';
    import { Http, Response } from '@angular/http';
    import {Observable} from 'rxjs/Rx';
    import 'rxjs/add/operator/map';


    @Injectable()
    export class SampledataService {

      private _url = 'assets/data/sampledata.json'

      constructor(private _http: Http) { }

      getData(){

        return this._http.get(this._url)
          .map((resSampleData: Response) => resSampleData.json());
      }

    }

sampledata.component.ts:

import { Component, OnInit } from '@angular/core';
import { SampledataService } from '../sampledata.service'

@Component({
  selector: 'app-sampledata',
  templateUrl: './sampledata.component.html',
  styleUrls: ['./sampledata.component.css']
})
export class SampledataComponent implements OnInit {

  sampledata = [];
  sampleDataModel = [];
  sampleDetailModel = [];

  constructor(private _sampledataservice: SampledataService) { }

  ngOnInit() {  

    this._sampledataservice.getData()
      .subscribe(resData => { 
        this.sampledata = resData;
        this.sampleDataModel = resData.Model.SampleDataModel;
        this.sampleDetailModel = resData.Model.SampleDetailModel });

  }

}

sampledata.component.html:

<h3>{{sampledata}}</h3>
<p>{{sampleDataModel.Title}}</p>
<p>{{sampleDetailModel.name}}</p>

My Questions is:

How do I get these values to display?

If anyone of you have any idea on how to solve this issue, please help to suggest the solutions to me, thank you.

3
  • 1
    try console.log on whole object. you will see sampledata.json has Model array( not Object) and in that array SampleDataModel and SampleDetailModel is also arrays not object. Commented Mar 26, 2018 at 21:23
  • Yes how do i convert it to object? Commented Mar 26, 2018 at 21:38
  • by using *ngFor loop Commented Mar 27, 2018 at 14:01

3 Answers 3

3
<h3>{{sampledata | json}}</h3> // pipe | json needed to show object as stringyfied
<p>{{sampleDataModel[0]?.Title}}</p> // ? to check if object is not null or undefined
<p>{{sampleDetailModel[0]?.name}}</p>

And change like below

this.sampledata = resData;
this.sampleDataModel = resData.Model[0].SampleDataModel;
this.sampleDetailModel = resData.Model[0].SampleDetailModel
Sign up to request clarification or add additional context in comments.

10 Comments

{{sampledata |json}} worked. Thanks. Is there a better way other than adding "|json" on html? <p>{{sampleDataModel?.Title}}</p> <p>{{sampleDetailModel?.name}}</p> didn't work
display properties one by one
Now I get "ERROR TypeError: Cannot read property '0' of undefined". Also how I stringyfy in service.ts or in component?
first try with <pre>{{sampleDataModel| json}}</pre> & <pre>{{sampleDetailModel| json}}</pre>
It did only display "&". No console errors @vinayakj
|
1

maybe you should try this: in ngOnInit when getting back your data:

this._sampledataservice.getData()
    .subscribe(resData => { 
    this.sampledata = resData;
    this.sampleDataModel = resData.Model[0].SampleDataModel;
    this.sampleDetailModel = resData.Model[0].SampleDetailModel });

... and in HTML:

<h3>{{sampledata}}</h3>
<p>{{sampleDataModel[0].Title}}</p>
<p>{{sampleDetailModel[0].name}}</p>

3 Comments

Thank you. This worked. one last question. Is it good to use Model[0] or sampleDataModel[0] ? I might add few more data later
What i usually do is make a strongly typed model, map it with data from server and bind it to the view and allways tryin to not use [0] or anything like that directly in html. Hope it helps.
How do I do that?
1

Few observations as per the code in OP :

  1. Access SampleDataModel

    To access SampleDataModel property, It should be resData.Model[0].SampleDataModel instead of resData.Model.SampleDataModel.

  2. {{sampleDataModel.Title}} ERROR TypeError: Cannot read property 'Title' of undefined

    To access Title property of a SampleDataModel array. It should be {{sampleDataModel[0].Title}} instead of {{sampleDataModel.Title}}.

    this.sampleDataModel = resData.Model[0].SampleDataModel;
    
  3. {{sampledata}} displaying [object Object]

    As sampledata is a JSON object it is displaying as [object object]. Stringify it before use in the template.

    this.sampledata = JSON.stringify(resData);
    

Comments

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.