1

I'm building a getUserByEmail informations page. I can have the response, but I am getting an error when I try to add html.

Here is my .html:

<ion-item>
  <ion-label>Email:</ion-label>
  <ion-input [(ngModel)]="keyword" type="text" value=""></ion-input>
</ion-item>
<button (click)="getCustomerbyEmail()">Get Customer</button>

THIS IS WORKING
<p>
  {{response | json}}
</p>
THIS IS NOT WORKING
(...)
<ion-list-header text-center>
      Modifier {{response.username}}
</ion-list-header>
(...)

My customer.ts:

import {Page, NavController} from 'ionic-angular';
import {Http} from 'angular2/http';
import {Inject} from 'angular2/core'
import {CustomerService} from './customers.service';

@Page({
  templateUrl: 'build/pages/ecommerce/customers/customers.html',
  providers: [CustomerService]
})
export class CustomersPage {

  response:string;
  keyword:string;
  results:string [];
  allCustomers:boolean = false;
  http;

  constructor(public nav: NavController,  httpService:Http, private customerService:CustomerService) {
    this.customerService = customerService;
    this.http = httpService;
    this.keyword = '';
  }

  userPressedCancel() {
      this.keyword = '';
  }

  keyHasBeenPressed(e) {
      if (e.keyIdentifier === 'Enter') {
          this.customerService.getCustomersById(this.keyword)
          .subscribe(
              response => this.response = response,
              error => console.log(error));
      }
  }

  getCustomerbyEmail() {
      this.customerService.getCustomersById(this.keyword)
      .subscribe(
          response => this.response = response,
          error => console.log(error));
  }
}

And finally my customer.service.ts:

(...)
  getCustomersById(id) : Observable<any> {
    let headers = new Headers();
    return this._http.get(this._url+this.customers+'/email/'+id+this.urlBase, {
      headers : headers
    }).map(res => res.json().customer);
  }
(...)

If I do a getAllCustomers then iterate the *ngFor="#customer of response" it's working as it should.

What am I missing? Thanks. Yin

2
  • What does response look like when the callback passed to subscribe(...) is called? Commented May 30, 2016 at 9:59
  • I get this error: EXCEPTION: Error: Uncaught (in promise): EXCEPTION: TypeError: Cannot read property 'id' of undefined in [ {{response | json}} {{response.id}} in CustomersPage@23:3] ORIGINAL EXCEPTION: TypeError: Cannot read property 'id' of undefined ORIGINAL STACKTRACE: Commented May 30, 2016 at 10:27

1 Answer 1

1

Use the safe-navigation (Elvis) operator to not get an error when response is not yet set:

  Modifier {{response?.username}}
Sign up to request clarification or add additional context in comments.

13 Comments

Awesome this is working. Thank you.
Just one more thing, how to I display this response:{{response?.shipping_address.company}}?
Not sure what you mean with "display this resonse". {{response | json}} should display the whole response object.
I'm getting an error when trying to display response?.shipping_address.company but response?.id is ok.
What error exactly? (for displaying {{response?.shipping_address.company}}, right - didn't see that part of your comment previously)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.