1

I have implement a method to retrieve data from HTTP request and it's working fine and return complex data list.

enter image description here

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.

enter image description here

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');
  }
  
}

3
  • This behavior makes no sense. Are you sure it's not just a glitch with your debugger not being on the line it thinks it's on? What do you see if you console.log both data and this.friendList? Commented Aug 27, 2018 at 22:54
  • @MattMcCutchen Yes data displaying if I use console.log but can't see in debugger . But problem is, If I need to access that this.friendList in other place suck as inside other function it shows as undefined Commented Aug 28, 2018 at 6:14
  • This may be the classic problem of trying to use the result of an asynchronous operation before it completes. Have you read stackoverflow.com/questions/14220321/… ? Commented Aug 28, 2018 at 13:09

2 Answers 2

1

It's a debugger bug. It gets confused by this. If you were to debug the generated javascript you'd get the right values, but in your case, the debugger resolves this to window, which most likely won't have a friendList field or property resulting in displaying "undefined". And this is why I never debug typescript code, but the generated javascript instead. Not saying you shouldn't, just be aware of that caveat.

Sign up to request clarification or add additional context in comments.

Comments

1

Issue have been solved.. :D It worked after I changed like this.

 getAllFriendsFromDatabase() {
    var self = this;
    this.userService.getUsers().subscribe(
      data => {
        self._moderatorList = data as ChatUserVM[];
        console.log('DB usersss ---> ' + self._moderatorList);
      }
    );
  }

1 Comment

You shouldn't be doing that in typescript, as that's kind of what it does when it generates the javascript code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.