typescript - Observable on AngularJS does not interpolate: " cant find property of undefined" -
everyone. have application shows cats. gets data json using service , http. can do, , works. however, have feature "cat detail" page, involves routing. uses same service not render whole json file, particular object, in case, particular cat. search said particular cat using find() method on first getcat() function, using route.params value search parameter.
however, routing isn't problem itself. can see below. console can print return of function , shows sole object should . problem seems concern moment of interpolation. when bind variable, component template, should carry data of observable, angular shows me "cannot read property 'nombre' of undefined".
i don't know why isn't working. people recommend using safe navigation operator on binding, doesn't me. same error keeps appearing.
i have rechecked syntax various days, , belief got right. makes me think problem relies on has scope of component, maybe related lifecycle hooks.
any appreciated.
this json
[ { "nombre": "xiomara", "edad": "1", "raza": "criolla", "color": "café", "vacunacion": "todas", "estado": "esperando", "foto" :"-104827/" }, { "nombre": "deneb", "edad": "4", "raza": "siamés", "color": "negro", "vacunacion": "falta", "estado": "esperando", "foto" : "" { "nombre": "pilatos", "edad": "2", "raza": "dragoon", "color": "pardo", "vacunacion": "azul", "estado": "todas", "foto" : "" }, { "nombre": "ulysses", "edad": "1", "raza": "criollo", "color": "gris", "vacunacion": "falta", "estado": "esperando", "foto" : "rt-fur-cat-104827/" }, { "nombre": "raiku", "edad": "1", "raza": "pokemon", "color": "rayas blancas", "vacunacion": "todas", "estado": "esperando", "foto" : "http-white-short-fur-cat-104827/" } ]
this service.ts
@injectable() export class gatosservice { private _producturl ='./lista gatos.json'; constructor(private _http: http) { } getgatos(): observable<igato[]> { return this._http.get(this._producturl) .map((response: response) => <igato[]>response.json()) .do(data => console.log('all: ' + json.stringify(data))) .catch(this.handleerror); } /* searches array objects match id*/ getgato(id: string): observable<igato> { return this.getgatos() .map((gatos: igato[]) => gatos.find(p => p.nombre === id)) .do(data => console.log('gato individual: ' + json.stringify(data))); /*prints individual object on console. work */ } private handleerror(error: response) { console.error(error); return observable.throw(error.json().error || 'server error'); } }
this detailcomponent.ts
export class gatodetallecomponent implements oninit { pagetitle: string = 'gato detalle'; gato: igato; errormessage: string; private sub: subscription; constructor( private _route:activatedroute, private _gatosservice: gatosservice ) { console.log(this._route.snapshot.params['id']) /* prints id know works */ } ngoninit(): void { /* calls service function using route.params argument */ this.sub = this._route.params.subscribe( params => { let id = params['id']; this.getgato(id); } ) } /* subscribes service getgato function , assigns variable gato*/ getgato(id: string) { this._gatosservice.getgato(id).subscribe( product => this.gato = product, error => this.errormessage = <any>error); } }
this detail component.html
<table> <tr><span class="visor-propiedad"> nombre: </span><span class="visor-valor">{{gato.nombre}}</span></tr> <tr><span class="visor-propiedad">edad: </span><span class="visor-valor">{{gato.edad}}</span></tr> <tr><span class="visor-propiedad">raza: </span><span class="visor-valor">{{gato.raza}}</span></tr> <tr><span class="visor-propiedad">color: </span><span class="visor-valor">{{gato.color}}</span></tr> <tr><span class="visor-propiedad">vacunacion: </span><span class="visor-valor">{{gato.vacunacion}}</span></tr> <tr><span class="detalle" [routerlink]="['/lafamilia', gato.nombre]">conóceme!</span></tr> </table>
this error shows on browser console
exception: uncaught (in promise): error: error in ./gatodetallecomponent class gatodetallecomponent - inline template:13:38 caused by: cannot read property 'nombre' of undefined error: error in ./gatodetallecomponent class gatodetallecomponent - inline template:13:38 caused by: cannot read property 'nombre' of undefined
browser console print object want. problem arrises believe in interpolation.
gato.service.ts:27 gato individual: {"nombre":"xiomara","edad":"1","raza":"criolla","color":"café","vacunacion":"todas","estado":"esperando","foto":"https://www.pexels.com/photo/grey-and-white-short-fur-cat-104827/"}
the problem html code executing before value assigned gato
. add ?
befor goto
inside interpolation this:
<table> <tr><span class="visor-propiedad"> nombre: </span><span class="visor-valor">{{gato?.nombre}}</span></tr> <tr><span class="visor-propiedad">edad: </span><span class="visor-valor">{{gato?.edad}}</span></tr> <tr><span class="visor-propiedad">raza: </span><span class="visor-valor">{{gato?.raza}}</span></tr> <tr><span class="visor-propiedad">color: </span><span class="visor-valor">{{gato?.color}}</span></tr> <tr><span class="visor-propiedad">vacunacion: </span><span class="visor-valor">{{gato?.vacunacion}}</span></tr> <tr><span class="detalle" [routerlink]="['/lafamilia', gato?.nombre]">conóceme!</span></tr> </table>
Comments
Post a Comment