1

I have an application in Angular 2. I making a http request but doesn't work.

this is the error

zone.js:2263 GET http://mtz-vweb7/portalEA/api/sistemas/undefined/tecnologias 404 (Not Found) core.es5.js:1020 ERROR SyntaxError: Unexpected end of JSON input

my service

import { ActivatedRoute, Params } from '@angular/router';
import { Http, Response, Headers, RequestOptions } from '@angular/http';
import { Observable } from 'rxjs/Rx';
import { Injectable, Inject } from '@angular/core';

@Injectable()
export class TecnologiaService {

    private _apiUrlSistema = 'http://mtz-vweb7/portalEA/api/sistemas';
    private route_id: string;

    constructor( @Inject(Http) private _http: Http,
        @Inject(ActivatedRoute) private routeTecnologia: ActivatedRoute) {
        console.log('rota no servico', routeTecnologia.snapshot.params.id);
        this.route_id = routeTecnologia.snapshot.params.id;
    }

    getTecnologias(): Observable<Response> {
        return this._http.get(this._apiUrlSistema + '/' + this.route_id + '/tecnologias')
            .map(res => res.json())
            .catch(this.throwError);
    }
    id() {
        return this.route_id;
    }
    private throwError(Response) {
        return Observable.throw(Response.json().error || 'Server error');
    }
}

my component

import { TecnologiaService } from './tecnologia.service';
import { ActivatedRoute, Params } from '@angular/router';
import { Component, OnInit, Inject } from '@angular/core';


@Component({
  selector: 'app-tecnologias',
  templateUrl: './tecnologias.component.html',
  styleUrls: ['./tecnologias.component.scss']
})
export class TecnologiasComponent implements OnInit {
  listaTecnologias;
  private idDaRota;
  constructor( @Inject(TecnologiaService) private sistemaService: TecnologiaService,
    @Inject(ActivatedRoute) private route: ActivatedRoute) {
      console.log('rota no componente', this.route);
    }

  ngOnInit() {
    this.sistemaService.getTecnologias()
      .subscribe(tecnologia => this.listaTecnologias = tecnologia[0]);

    this.idDaRota = this.sistemaService.id;
  }
}

I put a log to show me my activated route and return then

    snapshot
:
ActivatedRouteSnapshot
children
:
(...)
component
:
ƒ DetalheSistemaComponent(service, route)
data
:
{}
firstChild
:
(...)
fragment
:
undefined
outlet
:
"primary"
paramMap
:
(...)
params
:
id
:
"38011"

so I take the id but show me undefined and I don't know why.

1

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.