Angular return a error : Error: Uncaught (in promise): TypeError: res.users.subscribe is not a function.
Since this morning, I don't understand what's wrong in my resolve.
UserService.ts
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
@Injectable()
export class UsersService {
private api = 'http://localhost:3000/users';
private headers = new HttpHeaders();
constructor(
private http: HttpClient
) {}
getAllUsers(): Observable<any[]> {
return this.http.get<any[]>(this.api, {headers: this.headers, responseType: 'json' });
}
}
UserResolve.ts:
import { Injectable } from '@angular/core';
import { Resolve } from '@angular/router';
import { UsersService } from '../services/users.service';
@Injectable()
export class UsersResolve implements Resolve<any> {
constructor(private usersService: UsersService) {}
resolve() {
return this.usersService.getAllUsers();
}
}
UserComponent.ts
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import {Users} from '../_shared/models/Users';
@Component({
selector: 'app-user',
templateUrl: './users.component.html',
styleUrls: ['./users.component.sass']
})
export class UsersComponent implements OnInit {
constructor(private route: ActivatedRoute) {}
public title: string;
public users: Users[] = [];
ngOnInit() {
this.route.data.forEach((res: any): any => {
this.title = res.title;
res.users.subscribe(users => {
console.log(users);
this.users = users;
});
});
}
}
When I log res.users, it return "function UsersResolve()" with not proto subscribe...
The json is Array of Object like :
{
id: 13246,
guid: '46ffgd456dfs',
name: 'John Doe',
adress: ...
}
Can the problem come from the contents of my json ?
Originally, I wanted to train on the RxJS operator...
data.users
, it will not actually not run that way. See Pavan's answer. – Jens Neubauer