0

I'm doing a new service on my Angular 4 app but it does not call the back end, and I do not know what it can be:

//ANGULAR 4

findTipoProducto(): Observable<Combo> {
    console.log("entro en findTipoProducto");
    return this.http.get(`${this.resourceUrl}/bytipo`);
}

//API REST SPRING

@GetMapping("/combos/bytipo")
@Timed
public ResponseEntity<List<ComboDTO>> getByTipo() {
    log.debug("REST request to get Combos by Tipo");
    List<ComboDTO> list = comboService.findByTipo(TipoCombos.PRODUCTO);
    return new ResponseEntity<>(list, HttpStatus.OK);
}

The project is operational and the rest of things work, it is some syntax in the two pieces of code I have put up.

Thank you very much, if anyone can help me.

Jose
  • 47
  • 7
  • 3
    Are you subscribing to this request anywhere? (and map ??) – AT82 May 20 '17 at 13:12
  • Thank you I was missing the subcribing: this.comboService.findTipoProducto().subscribe( result => { console.debug(result); }, error => { console.debug(error); }); – Jose May 20 '17 at 16:58
  • 1
    Possible duplicate of [Angular 2 http get not getting](http://stackoverflow.com/questions/41381200/angular-2-http-get-not-getting) – AT82 May 20 '17 at 17:02
  • 1
    Yes, yeah request will not fire unless there is a subscriber (as can be seen from the duplicate). But glad to hear there was this easy solution to your issue :) Have a good one and happy coding! :) – AT82 May 20 '17 at 17:04
  • 1
    It was the first time I did it, and the last thing I would have thought is that you have to do a "subcribe" on the controller. Thank you very much. – Jose May 20 '17 at 17:22

1 Answers1

0

In service:

 findTipoProducto(): Observable<Combo> {    
    return this.http
            .get(url,photo,options)
            .map( res => { return res; } )
            .catch((err:Response) => {
                return Observable.throw(err);
           });
      }

In component :

import {YourServiceName} from 'path';
import { Component} from '@angular/core';
import { Router } from '@angular/router';

    export class YourComponent{

    Declare your bodyResponseAsObject

    ...

     constructor(
        private router:Router,
        private yourServiceName:YourServiceName
       ) { }

    ...

        onSubmit(){
            let res = this.yourServiceName.findTipoProducto();
            res.subscribe(
              data=> { this.bodyResponseAsObject = JSON.parse(data.text()) },
              err => 
              {
                this.router.navigate(['/',err.status]);
              }
            );
    }

HTML : fire the onSubmit method on button input for example

RazvanParautiu
  • 2,805
  • 2
  • 18
  • 21