5

I use Angular5 with httpClient to request a rest api. My issue is I perfectly get lists of objects but I can't obtain a single object. I use modules for this module I have a service in which I have my call to the api-rest:

    getUser(id: string) {
    return this.http.get<IUserComponent>('apiAddress/getbyid?id=' + id);
    }

In the component I call it

    ngOnInit() {
    this.id = this.route.snapshot.paramMap.get('id');
    this.getUser(this.id);
    }
    getUser(id): void {
    this.userService.getUser(id).subscribe(data => {
        this.user = data;
    });
    }

And I call user in my template:

    <p>
      {{user.name}}
    </p>

When I do that I get nothing displayed and the error

    TypeError: _co.user is undefined
    ERROR CONTEXT Object { view: Object, nodeIndex: 12, nodeDef: Object, elDef: Object, elView: Object }

I can get something displayed but I still get the error if in the service I say I want an array

    getUser(id: string) {
    return this.http.get<IUserComponent[]>('apiAddress/getbyid?id=' + id);
    }

and when I call it I say

    ngOnInit() {
    this.id = this.route.snapshot.paramMap.get('id');
    this.getUser(this.id);
    }
    getUser(id): void {
    this.userService.getUser(id).subscribe(data => {
        this.user = data[0];
    });
    }

And finally if I call the service:

    getUser(id: string) {
    return this.http.get<IUserComponent[]>('apiAddress/getbyid?id=' + id);
    }

and call it

    ngOnInit() {
    this.id = this.route.snapshot.paramMap.get('id');
    this.getUser(this.id);
    }
    getUser(id): void {
    this.userService.getUser(id).subscribe(data => {
        this.users = data;
    });
    }

I can display the data with *ngFor and don't get any error. I don't very understand why.

In the hero tutorial of Angular https://angular.io/tutorial/toh-pt6 They wrote this service to get all:

      getHeroes (): Observable<Hero[]> {
      return this.http.get<Hero[]>(this.heroesUrl)
      .pipe(
      tap(heroes => this.log(`fetched heroes`)),
      catchError(this.handleError('getHeroes', []))
      );
      }

and this one to get a single one

    getHero(id: number): Observable<Hero> {
    const url = `${this.heroesUrl}/${id}`;
    return this.http.get<Hero>(url).pipe(
    tap(_ => this.log(`fetched hero id=${id}`)),
    catchError(this.handleError<Hero>(`getHero id=${id}`))
    );
    }

where the pipe is used to do several action so here tap and cathError and tap I am not sure to understand correctly what it is used for there is a doc https://github.com/ReactiveX/rxjs/blob/5.5.2/src/operators/tap.ts#L14 but it seems to be used for debug.

Niradnik
  • 156
  • 1
  • 10
  • 2
    if you were to look at the example html, you would also see that they are using `
    ` in template :)
    – AT82 Dec 22 '17 at 09:47
  • Also apparently you have not initialized `user`, therefore it's undefined. I suggest you also read this: https://stackoverflow.com/questions/34734671/observable-type-error-cannot-read-property-of-undefined Not looking too closely on your code, so hopefully there is no additional issues :) – AT82 Dec 22 '17 at 09:49
  • IUserComponent? it's a strange name for an interface that must be an interface of only data. NOTE: Use {{user?.name}} or initialize the variable – Eliseo Dec 22 '17 at 10:04
  • I don't understand why I would need to initialize user, if I don't need to initialize users. If by initializing you mean give it an initial value I initialize non of them if you mean define their type it is done for both of them. – Niradnik Dec 22 '17 at 10:14
  • The difference between why needing to initialize `user` but not `users`, is tho both are `undefined`, but you are trying to read **property** `name`. And there is no such property on `undefined` :) – AT82 Dec 22 '17 at 11:45
  • thanks AJT_82 the *ngIf corrected my error in the console so I can get my value but I have to retrieve an array and then say I take the first element. If I want to get the object directly I get nothing if I do an alert to get the name I get undefined so I guess it is because it is not initialized as you say. thanks again :) – Niradnik Dec 22 '17 at 13:09

0 Answers0