After retrieving data from an HTTP request, I am storing it in an array within the same service.
export class UserService {
myusers: User[];
constructor(private http: HttpClient) {}
getUsers () {
return this.http.get<User[]>('https://randomuser.me/api/results=5')
.subscribe( data => { this.myusers = data['results']}); }
}
Despite storing the data, when I console log myusers in component.ts, it shows 'Undefined'.
export class UserComponent implements OnInit {
constructor(private service: UserService) { }
ngOnInit() {
this.service.getUsers();
console.log(this.service.myusers); //undefined
}
}
Is there a way to access the this.service.myusers
without resorting to storing the data in the component's array instead of the service's myusers array?