I have implement a method to retrieve data from HTTP request and it's working fine and return complex data list.
But my concern is when I assign that returned list to variable,It's not assigning.So I can't even loop that variable because it's undefined in .ts file.
This is my Component
import { Component, OnInit } from '@angular/core';
import { ChatUserVM } from '../shared/models';
import { UserService } from '../shared/service/user.service';
@Component({
selector: 'app-chat-footer',
templateUrl: './chat-footer.component.html',
styleUrls: ['./chat-footer.component.css'],
})
export class ChatFooterComponent implements OnInit {
friendList: ChatUserVM[] = [];
constructor(private userService: UserService) {
}
ngOnInit() {
this.getAllFriendsFromDatabase();
}
getAllFriendsFromDatabase() {
this.userService.getUsers().subscribe(
data => {
this.friendList = data;
console.log('DB usersss ---> ' + this.friendList);
}
);
}
}
This is my HTTP service
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import {ChatUserVM} from "../models";
@Injectable()
export class UserService {
constructor(private http: HttpClient) { }
getUsers() {
return this.http.get<ChatUserVM[]>('https://localhost:44346/api/chat/GetAllUsers');
}
}