2017-04-07 44 views
1

我實現了這個dunction:RxJS:地圖功能沒有達到

public deleteCards(user: string, tokens: Array<string>, extraHttpRequestParams?: any): Observable<{}> { 
    return this.deleteCardsWithHttpInfo(user, tokens, extraHttpRequestParams) 
     .map((response: Response) => { 
      if (response.status === 204) { 
       return undefined; 
      } else { 
       return response.json(); 
      } 
     }).catch((error: any) => { 
      if (error.status >= 500) { 
       return Observable.throw(new Error(error.status)); 
      } 
      else { //if (error.status >= 400) { 
       const body = error.json() || ''; 
       const code = body.error || JSON.stringify(body); 
       const message = body.message || JSON.stringify(body); 
       return Observable.throw(ApiError.create(code, message)); 
      } 
     }); 
} 

正如你所看到的,它返回一個Observable<{}>。我打電話是因爲:

this.deleteCards(action.payload.username, action.payload.tokens) 
     .map(() => { ((((1)))) 
     return <Action>{ 
      type: 'DELETE_CARDS_SUCCESS', 
      payload: action.payload.tokens 
     }; 
     }) 
     .catch(_ => { 
     return Observable.of(<Action>{ type: 'DELETE_CARDS_FAILED', payload: { }}); 
     })); 

然而,從未達到((((1))))

我也試過這個:

  1. .map(_ => { ...
  2. .map((p:any) => {...

,但它從來沒有達到過。

+0

嗯...我們怎麼能知道呢?可能this.deleteCardsWithHttpInfo()可能永遠不會發出任何東西。 – martin

回答

0

你必須訂閱的觀察到的,它做任何事情:

this.deleteCards(...).map(...).catch(...).subscribe((action: Action) => { 
    // do something with the action. 
});