Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 .subscribe() returning undefined

Tags:

angular

In component, i called dataservice method 'login' which calls post api. res.json() is giving correct response. But in component, when i try to access 'data' it is coming as undefined in subscribe. Don't know what i am doing wrong here. Please help.

dataService:

login(username, password): Observable<any>{       
    let data = { username: username, password: password};
    return this.http.post( this.domain + '/webservice/login', data)
                    .map( (res: Response) => {
                      res.json()                      
                    })
                    .catch( (error: any) => Observable.throw(error.json().error || 'server error') );

  }

component:

callLoginApi() {
    this.showLoading = true;
    this.dataService.login(this.username,this.password)
         .subscribe(data => {
              console.log(data);
          });
  }  
like image 948
Vivek Avatar asked Jul 10 '26 06:07

Vivek


1 Answers

Replace

.map((res: Response) => {
  res.json()                      
})   

by

.map((res: Response) => {
  return res.json();
})  

or by

.map((res: Response) => res.json())  
like image 137
JB Nizet Avatar answered Jul 15 '26 11:07

JB Nizet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!