2017-01-22 6 views
1

私は、サービスが失敗したときにリクエストペイロードを渡す必要があるシナリオを持っています。私のコードは以下のようになります。ngrx /効果がデータをオペレータに渡す

@Effect() doGetEvents$: Observable<Action> = this.actions$ 
.ofType(EVENTS) 
.switchMap((action) => { 
    let eventDate = action.payload.date; 
    return this.http.service(action.payload); 
}) 
.map(res => { 
    // success 
    if (res.status) { 
    return CustomActions.prototype.eventsResponse({ type: EVENTS_RESPONSE, payload: res.payload }); 
    } 

    //failure 
    return CustomActions.prototype.EventsErrorResponse({ 
    type: CustomActions.EVENTS_ERROR_RESPONSE, 
    payload: { 
     status: res.status, 
     errorMessage: res.errorMessage, 
     billDate: '10/01/2016', // <--- I need the eventDate got from the above switchMap 
     errorType: CustomActions.EVENTS + '_ERROR' 
    } 
    }); 

}); 

私は

.switchMap((action) => { 
    let eventDate = action.payload.date; 
    return [eventDate, this.http.service(action.payload)]; 
}) 

のように渡してみましたが、これはhttp呼び出しを実行しないであろうと.map()引数に応答を返しません。

eventDateを効果の範囲外にしてサービスが失敗したときに割り当てますが、それはよりクリーンな方法ではありません。

答えて

0

あなたはペイロードからの情報を含める場合は、HTTPサービスの結果と一緒に、あなたはこのようなmap演算子、使用することができます。

.switchMap((action) => { 
    return this.http 
    .service(action.payload) 
    .map(result => [action.payload.date, result]); 
}) 
.map(([date, result]) => { ... }) 
+0

を、これは私が探していたもので、私はdidnのことを私の悪いです「通常の」HTTPサービスの方法ではなく、Observableの方法で考える:D – yashhy

関連する問題