Pass data from Console to URL

Viewed 84

When I cklick on a button I get the right data in the console, but now when I click on the button I want that the data 'reid' should pass to the url. When I try:

this.router.navigateByUrl('/details/' + this.results.reid);

I get undefined back.

enter image description here

home.page.ts

searchName() {
    this.results = this.userService.getName(this.searchTermName).subscribe(result => {
        console.log('Restaurant: ', result);
        this.router.navigateByUrl('/details/' + this.results.reid);
        });
  }

home.page.html

<ion-grid >
        <ion-row >

            <ion-col size-xs="12" size-sm="12" size-md="8" size-lg="8">
                <ion-searchbar placeholder="Restaurant" [(ngModel)]="searchTermName" ></ion-searchbar>
            </ion-col>
        </ion-row>
</ion-grid>

<div class="btn" >
  <ion-button (click)="searchName()"color="primary" fill="solid">
       suchen
  </ion-button>
</div>

user.Service.ts

 getName(name1) {
    return this.http.get('http://127.0.0.1:8000/searchRestaurantByName', {headers: {name: name1}}).pipe(
        map(name => {
            console.log('RAW: ', name1);
            return name;
        })
    );
  }
3 Answers

results is an array, so you should use:

this.router.navigateByUrl('/details/' + result[0].reid);

Notice that you have a typo, it is result not results if I see corectly.

I see that this.results is an array of object, its normal that you get undefined, try to use find wich object first after make your redirect. for test you can use result[0].reid you should get your 39.

this.results is a subscription. It does not contain any 'reid' property.

What you should do is

  searchName() {
    this.userService.getName(this.searchTermName)
     .subscribe(result => {
       this.router.navigateByUrl('/details/' + result[0].reid);
     });
  }
Related