Wesley
Wesley

Reputation: 92

Ionic 2 / Angular - Manipulating JSON

I'm have a trouble to list a json file.

My code:

    this.http
      .post(link, data, options)
      .map(res => res.json().d)
      .subscribe(
          data => {
            this.profile = JSON.parse(data);
            console.log(this.profile);
          },
          err => {
            console.log("ERROR!: ", err);
          }
      );


Console.log print this:

Object {user: Object, class: Array(1)}

{  
   "user":{  
      "name":"Name",
      "date":"19880210",
      "email":"[email protected]",
      "about":"About me",
      "picuteUrl":"http://www.url.com/Midia/User/picture.jpeg",
   },
   "class":[  
      {  
         "id":"82",
         "name":"Class 01"
      }
   ]
}

I can also get these values in the TypeScript file

console.log(this.profile.user.name);
console.log(this.profile.class[0]);
console.log(this.profile.class[0].name);

But in html code i dont get this values.

<input type="text" value="{{profile.user.name}}">

ERROR:

ion-dev.js?v=1.1.4:156 TypeError: Cannot read property 'usuario' of undefined

Upvotes: 2

Views: 44

Answers (1)

Sajeetharan
Sajeetharan

Reputation: 222542

There is no where you are accessing, usuario in the provided code. You could use elvis/safe operator to check if the valaues are present

<input type="text" value="{{profile?.user?.name}}">

Upvotes: 1

Related Questions