I have this url=https://reqres.in/api/users?page=2 and I am using a get request to fetch the data.
user.ts is an interface
export interface User {
id:number,
email:string,
first_name:string,
last_name:string,
avatar:string
}
user.service.ts is a service
getAllUsers() : Observable<User[]> {
return this.http.get<User[]>("https://reqres.in/api/users?page=2");
}
user.component.ts
import { Component, OnInit } from '@angular/core';
import { UserService } from '../user.service';
@Component({
selector: 'app-user',
templateUrl: './user.component.html',
styleUrls: ['./user.component.css']
})
export class UserComponent implements OnInit {
constructor(private userService: UserService) { }
public user =[];
ngOnInit(): void {
this.userService.getAllUsers().subscribe(data => this.user = data)
}
}
I want to get the data object from the response object of 'GET' request and make a array of users.
Error: src/app/user/user.component.ts:16:54 - error TS2322: Type 'User[]' is not assignable to type 'never[]'.
16 this.userService.getAllUsers().subscribe(data => this.user = data)
~~~~~~~~~
Error: src/app/user/user.component.ts:16:54 - error TS2322: Type 'User[]' is not assignable to type 'never[]'.
Type 'User' is not assignable to type 'never'.
16 this.userService.getAllUsers().subscribe(data => this.user = data)
get(...).subscribe(response => console.log(response))data.datathat you want.public user: User[] = []should do the trickuserbefore ngOnInit anyways?