1

I am new in Angular2.I am trying to learn how parse JSON.When I search about this, noticed this problem.Please explain me,why it returns undefined after assign it JSON.stringfy()'s returned value??

import {Component} from '@angular/core'
import {User} from './model/user'
import {LoginService} from "./login.service";

 @Component({
 selector:'login-component',
 templateUrl: './login.html',
 providers:[LoginService]
})

export class LoginComponent{
  constructor (private loginService:LoginService) { }
  result:string;

  goToMine(username:string,password:string){
    this.loginService.checkUser(new User(username,password)).subscribe(
      myData=> this.result=JSON.stringify(myData),

      error=> alert(error),
      ()=> console.log("Finished")
    );

    console.log(this.result);//why does it print undefined??
  }
}
Vusala Hasanli
  • 409
  • 2
  • 7
  • 21
  • This question has been asked about a hundred times. The canonical answer is http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call. –  May 09 '17 at 10:32
  • Yes, I have read about this a lot.But may be I didn't know exact reason of this problem. After I read duplicate question,all dots were connected – Vusala Hasanli May 09 '17 at 10:44
  • Thank you @echonax – Vusala Hasanli May 09 '17 at 10:44

0 Answers0