Load log by id in Angular

Asked

Viewed 428 times

0

This below is a brief explanation.

By clicking the register on browser screen I can not load the records according to the ID.

This below is a detailed application;

View the gif file

inserir a descrição da imagem aqui

You can notice that there are two records being loaded and by clicking on the records it is able to load in the URL the registration ID, when viewing the console you notice that it lists the database records that are only two same.

The problem is that when placing on the page {{ restaurant?.name }} it does not print on the screen the value of the bank.

I don’t know what I’m doing wrong, this is my kind of services;

 restaurantById(id: string): Observable<Restaurant> {
    return this.http.get(`${this.url}/restaurants/${name}`)
    .map(response =>  response.json().restaurants)
    .catch(ErrorHandler.handlerError)
  }

This is my component class according to web page.

ngOnInit() {
    this.restaurantService.restaurantById(this.route.snapshot.params['id'])
    .subscribe(restaurant =>  this.restaurant = restaurant)
}

THIS IS MY REPOSITORY

sorry I put it in high box, because sometimes people do not realize that I have put the link from my repository.

The page is inside the package Restaurant-Detail and the service is the archive Restaurant.service.ts

I’ll be waiting for a comeback, I really need help.

SERVER RESPONSE

inserir a descrição da imagem aqui

  • Shouldn’t be ${id} here?? this.http.get(${this.url}/Restaurants/${name}`

  • If I put the as you are suggesting it does not load the bank records in the component class

  • But then you have to see the return of the API tbm, and adjust as needed... What is happening is that you are probably getting an array of restaurants (see response.json().restaurants), but tries to display only one.

  • In your class of service where you have .map(response => response.json().restaurants) shouldn’t be? .map(response => response.json())

  • @Marconi I tried your suggestion, but did not catch, still not appearing the records on screen.

  • @wladyband you started json-server?

  • 1

    Try it this way: restaurantById(id: string): Observable<Restaurant> {&#xA; return this.http.get(${this.url}/Restaurants/${id})&#xA; .map(response => response.json())&#xA; .catch(ErrorHandler.handlerError) }

Show 2 more comments

2 answers

3


Your method restaurantById is ignoring the parameter id, and using a variable that does not exist (name). This way he will make the requisition on /restaurants/ and list all restaurants.

This is evidenced by the need to use the .restaurants in the map method:

restaurantById(id: string): Observable<Restaurant> {
  return this.http.get(`${this.url}/restaurants/${name}`)
  .map(response =>  response.json().restaurants)
  .catch(ErrorHandler.handlerError)
}

According to the posted server response, the ideal implementation would be:

restaurantById(id: string): Observable<Restaurant> {
  return this.http.get(`${this.url}/restaurants/${id}`)
  .map(response =>  response.json().restaurant)
  .catch(ErrorHandler.handlerError)
}
  • It gave to help in parts, because now it does not load in the browser console all records it only loads the one that was clicked, but when I put {{ Restaurants?. name}} does not look like the on-screen record.

  • You can show how the server response is coming?

  • I just updated the post, please see now

  • @wladyband updated the response with the necessary mapping.

  • worked, thank you very much.

1

Try it this way:

Service

import { map } from 'rxjs/operators';
import { HttpClient } from '@angular/common/http';

constructor(private http: HttpClient) { }

restaurantById(id: string): Observable<Restaurant> {
  return this.http.get<Restaurant>(`${this.url}/restaurants/${id}`)
  .pipe(map(response =>  response.restaurant),
       catchError(ErrorHandler.handlerError)
  )      
}

Component

constructor(protected route: ActivatedRoute)

this.route.params.pipe(
        map(params => params['id']),
        switchMap(id=> this.restaurantService.restaurantById(id))
       .subscribe(restaurant => this.restaurant = restaurant );
  • could please tell me imports for the variables "map", "switchMap" and "params" ?

  • is generating this error message: Typeerror: operators_1.switchMap(...). subscribe is not a Function at Restaurantdetailcomponent.ngOnInit (Restaurant-Detail.component.ts:28) at checkAndUpdateDirectiveInline (core.js:12411) at checkAndUpdateNodeInline (core.js:13935) at checkAndUpdateNode (core.js:13878) at debugCheckAndUpdateNode (core.js:14771) at debugCheckDirectivesFn (core.js:14712) at Object.Eval [as updateDirectives] (Restaurantdetailcomponent_host.ngfactory.js? [Sm]:1) at Object.debugUpdateDirectives [as updateDirectives] (core.js:14697)

  • I am using rxjs 6 if you are using 5 are the same operators, only changes Imports and instead of putting inside pipe vc calls with . map inves de pipe(map)

  • Thank you so much for the effort you’re trying to help me, I’m very grateful.

  • Pay attention that in your service you are calling Response.Reststaurants

Browser other questions tagged

You are not signed in. Login or sign up in order to post.