Angular and Graphql

Asked

Viewed 27 times

-1

I have a problem with displaying the html component:

Query graphql in service:

document = gql`
  query unidadeMedida($id: ID!) {
    unidadeMedida(id: $id){
      id
      unidadeMedida
      descricao
    }
  }
`;

Interface:

export interface UnidadeMedida {
  id: number;
  unidadeMedida: String;
  descricao: String;
}

Method no ts:

getUnidade(id: number){
  this.unss = this.unidadeMedidaService2.watch({
    id: id
  })
  .valueChanges
  .pipe(
    map(result => result.data.unidadeMedidas)
  );
  console.log(`this.unss: ${this.unss}`);
}

Making the call:

this.getUnidade(3);

In html:

<tr *ngFor="let unidade of unss | async; let i=index, let first=first">
  <td> <span class="badge badge-primary badge-pill">{{ unidade.id}} </span> </td>
  <td>{{ unidade.unidadeMedida }}</td>
  <td> {{ unidade.descricao}} </td>

The request exits correctly to the server and the same responds with the requested data. And 200 ok.However, html does not display the data, someone has some view of what might be wrong?

Grateful!

  • In the console.log the data are shown correctly?

  • On the console displays: [Object Object]

  • So if the console shows [Object Object] you won’t be able to do the *ngFor, need to have an API return that is a array.

  • getUnidade(id: number): Observable<Unidademedida[]>{

  • The first time I made a function in the service that returns an array: getUnidade(id: number): Observable<Unidademedida[]>{ No ts an array receives the result: unss!: Unidademedida[]; . subscribe(data) => (this.unss = data); In Html is the interaction with *ngFor I made some changes and now the console displays:

No answers

Browser other questions tagged

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