2017-03-23 93 views
1

我有一個服務,並在角2應用程序中,我得到的服務,並能記錄也控制檯響應,但它顯示的成分不確定的一個組成部分。我在這裏做什麼錯了?任何人都可以指出這個錯誤。 在此先感謝角2訂閱觀察到越來越不確定

服務:

import { Injectable } from '@angular/core' 
import { Http, Response,Headers } from '@angular/http' 
import { Observable } from 'rxjs/Observable' 
import "rxjs/add/operator/map" 
import "rxjs/add/operator/catch" 
import "rxjs/add/Observable/throw" 

@Injectable() 
export class LoginService { 
    public logResult; 
    private _url = "http://localhost:53798/api/Login?uname=abcd&pass=1" 


    constructor(private _http: Http) { } 

getEmployees(userID:string,passWord:string) : Observable<boolean>{ 

    return this._http.get(this._url).map((response: Response) => 
     { 
      let logResult= response.json(); 
      logResult.forEach(re=>{ 

       if(re.LoginResult=="Success") 
       { 
        console.log('Success');//this is being written to console 
        return<boolean> true; 
       } 
       else{ 
        console.log('fail'); 
        return<boolean> false; 
       } 
      }) 
    }).catch(this._errorHandler); 


} 
_errorHandler(error: Response) { 
    console.log(error); 
    return Observable.throw(error || "Server not Found") 
} 
} 

及部件:

ValidateLogin(){ 


    this._loginService.getEmployees(this.loginForm.value.UserId,this.loginForm.value.password) 

    .subscribe(resLoginData => { 
     console.log(resLoginData);//resLoginData is undefined 
     if(resLoginData===true) 
     { 
      console.log('Success'); 
      this.router.navigateByUrl('/Home');} 
     else{ 
     console.log('Fail'); 
     this.router.navigateByUrl('/Login'); 
     } 
     resLoginError => this.errorMsg = resLoginError})   
}  
} 
+1

我認爲你沒有在'map'函數中正確返回響應,請嘗試像這樣'return logResult.forEach(re => {....' –

回答

2

forEach塊不會停止在return聲明。你

let logResult= response.json().data; 
let result = false; 
logResult.forEach(re=>{ 

    if(re.LoginResult=="Success") 
    { 
     // console.log('Success');//this is being written to console 
     result = true; 
    } 
    else{ 
     // console.log('fail'); 
     result false; 
    } 
}); 

return result;  // return the true result out of forEach. 

還應該更改let logResult= response.json();let logResult= response.json().data;用於HTTP的方法將返回的數據這種方式。

+0

感謝Pengyy它解決了問題,因爲你提到它不會返回來自foreach,再次感謝。:-) – user

0

你有沒有安裝rxjs和區域-JS在你的應用程序

import { Injectable } from "@angular/core"; 
     import { Http, Response, Headers, RequestOptions, URLSearchParams } from "@angular/http"; 
     import { Observable } from "rxjs/Observable"; 
     import "rxjs/Rx"; 

    @Injectable() 
    export class LoginService { 
     public logResult; 
     private _url = "http://localhost:53798/api/Login?uname=abcd&pass=1" 
    constructor(private _http: Http) { } 

    getEmployees(userID:string,passWord:string) : Observable<boolean>{ 

     return this._http.get(this._url).map((response: Response) => 
      { 
       let logResult= response.json(); 
       logResult.forEach(re=>{ 

        if(re.LoginResult=="Success") 
        { 
         console.log('Success');//this is being written to console 
         return true; 
        } 
        else{ 
         console.log('fail'); 
         return false; 
        } 
       }) 
     }).catch(this._errorHandler); 


    } 
    _errorHandler(error: Response) { 
     console.log(error); 
     return Observable.throw(error || "Server not Found") 
    } 
    }