I am trying to implement the routing in my app. My goal is simple: after clicking on one of the rows of my table, the app should show the specific details of the selected row (speedway rider in this case).
I'm stuck on the point where the path after click works correctly, but the page returns no data with the following information in the console:
ERROR TypeError: Cannot read property 'id' of undefined
I have a feeling that I almost there, but after hours of combinations I decided to ask for help. More code below, I would be grateful for any ideas :)
service.ts
getRider(id) : Observable<Rider> {
return this._http
.get("http://node.gurustats.usermd.net:60519/pgee2020" + `/${id}`)
.map(result => (this.result = result.json().data));
}
subpage.ts
import { Component, OnInit } from "@angular/core";
import { Rider } from "../interfaces/rider";
import { SpeedwayService } from "../../speedway.service";
import { ActivatedRoute } from "@angular/router";
@Component({
selector: "gs-zawpgee2020",
templateUrl: "./zawpgee2020.component.html",
styleUrls: ["./zawpgee2020.component.less"]
})
export class ZawPgee2020Component implements OnInit {
rider: Rider;
constructor(
private _speedwayService: SpeedwayService,
private route: ActivatedRoute){}
ngOnInit() {
this.loadZaw();
}
loadZaw() {
const id = +this.route.snapshot.params['id'];
this._speedwayService.getRider(id).subscribe((rider) => {
this.rider = rider;
})}}
subpage.html
<gs-header>
<p logo><img src="assets/logo PGEE.jpg" /></p>
<p levels>HISTORIA MECZÓW - {{ rider.id }}</p>
</gs-header>
data structure (server.js)
// 20210312220521
// http://node.gurustats.usermd.net:60519/pgee2020
{
"status": 200,
"message": null,
"data": [
{
"_id": "604a882ed87fdb0482536fc9",
"MSC": 3,
"ZAWODNIK": "Bartosz Zmarzlik",
"KLUB": "Gorzów",
"SREDNIA": 2.43,
"id": 103
...
[routerlink] & path: "pgee20/:id" are also coded
paramMapinsteadthis.route.paramMap.get('id'). According to Angularparamswill be deprecated. angular.io/guide/… Maybe it already has?Property 'get' does not exist on type Observable<ParamMap>snapshotso likethis.route.snapshot.paramMap.get('id');paramsinthis.route.snapshot.params['id']is theparamsproperty ofActivatedRouteSnapshotclass. The linked documentation is aboutparamsproperty ofActivatedRouteclass.