Syntax Error in MAP Angular6

0

I'm in an angular course 4, but I'm using 6, it catches me well, but every now and then there are some things that do not roll, I managed to fix most, but there's one that's making me crazy, MAP in the search method. I would need to move forward with this because my CBT depends on this kkkk. Thanks in advance.

Top Component (where the search bar is)

import { Component, OnInit } from '@angular/core';
import { OfertasService } from '../ofertas.service';
import { Observable } from 'rxjs';
import { Oferta } from '../shared/oferta.model';
 public pesquisa(termoDaPesquisa: string): void{
    this.ofertas = this.ofertasService.pesquisaOferta(termoDaPesquisa)
    this.ofertas.subscribe(
      (ofertas: Oferta[]) => console.log(ofertas)
  )
  }

oferta.service.ts (where is the logic of the offers, where I will do the search)

import { Http } from '@angular/http';
import { Injectable } from '@angular/core';
import { Oferta } from './shared/oferta.model';
import { URL_API } from './app.api'
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
public pesquisaOferta(termo: string): Observable<Oferta[]> {
        return this.http.get('${URL_API}?descricao_oferta_like=${termo}')
            .pipe(map((resposta: any) => resposta.json()))

    }

and the top component html

<input type="text" class="form-control" placeholder="Pesquise por ofertas..." #termoDaPesquisa (keyup)="pesquisa(termoDaPesquisa.value)"/>

And I'm going to show a print of the screen with the error!

EDIT: topo.component complete

import { Component, OnInit } from '@angular/core';
import { OfertasService } from '../ofertas.service';
import { Observable } from 'rxjs';
import { Oferta } from '../shared/oferta.model';

@Component({
  selector: 'xyz-topo',
  templateUrl: './topo.component.html',
  styleUrls: ['./topo.component.css'],
  providers: [OfertasService]
})
export class TopoComponent implements OnInit {

  public ofertas: Observable<Oferta[]>

  constructor(private ofertasService: OfertasService) { }

  ngOnInit() {
  }

  public pesquisa(termoDaPesquisa: string): void{
    this.ofertas = this.ofertasService.pesquisaOferta(termoDaPesquisa)
    this.ofertas.subscribe(
      (ofertas: Oferta[]) => console.log(ofertas)
  )
  }

}
    
asked by anonymous 06.09.2018 / 15:10

1 answer

0

There were changes in the map() method, from Angular version 4, to 6. I believe that in your case the problem is that there is no longer the conversion to JSON in the method map().

Try modifying the pesquisa() method in the offers.service.ts file.

public pesquisaOferta(termo: string): Observable<Oferta[]> {
    return this.http.get(URL_API + "?descricao_oferta_like=" + termo)
    .pipe(
        retry(10),
        map((response: Oferta[]) => {
            return response
        })
    )
}

Try also to pass all three parameters in your observer registration.

   public pesquisa(termoDaBusca: string) : void {

    // Observavel
    this.ofertas = this.ofertasService.pesquisaOferta(termoDaBusca)

    // Observador
    this.ofertas.subscribe(
      (ofertas: Oferta[]) => {
        console.log(ofertas)
      },
      (error: string) => {
        console.log("Erro na pesquisa. Erro: " + error)
      }, () => {
        console.log("Termino do fluxo da stream")
      })
  }
    
07.09.2018 / 16:31