2017-06-12 8 views
0

APIエンドポイントにファイルをアップロードするようにリクエストする必要があります。私はプロジェクト全体でアクシオスを使用していますが、ファイルを添付するには問題があるように見えますが、それはSuperagentでは簡単なはずです。しかし、私のSagaコードはSuperagent(応答オブジェクトなし、APIはトリガーされません)では動作しません。何が間違っていますか?Redux-SagaとSuperagent

import { delay } from 'redux-saga'; 
 
import { select, call, put } from 'redux-saga/effects'; 
 
import request from 'superagent' 
 
import * as action from '../../constants/actions/'; 
 
import config from '../../constants/config'; 
 
import { getFile, selectEntity, selectPreview } from '../../selectors'; 
 

 
export default function* putUserpic() { 
 
    const file = yield select(getFile) 
 
    const API = process.env.API_URL || config.API_URL; 
 

 
    const entity = yield select(selectEntity); 
 
    const requestURL = `${API}/${entity}/userpic`; 
 
    const token = localStorage.getItem(config.TOKEN); 
 

 

 

 
    var req = request.post(requestURL) 
 
    .attach(file.name, file) 
 
    .set('authorization', token); 
 

 

 
    try { 
 
     yield put({type: action.REQ_PENDING}); 
 
     const response = yield call(req.end) 
 
     yield put({type: action.RES_RECEIVED}) 
 
     yield put({type: action.MESSAGE, payload: response.data.message}); 
 

 
    } catch (e) { 
 
     yield put({type: action.RES_RECEIVED}) 
 
     yield put({type: action.AUTH_ERROR, payload: e.response.data.error}); 
 
     yield delay(config.MSG_DELAY); 
 
     yield put({type: action.RESET_ERROR}) 
 
    } finally { 
 
     yield delay(config.MSG_DELAY); 
 
     yield put({type: action.RESET_MESSAGE}) 
 
    } 
 
}

答えて

0

あなたはが約束を返す何かを呼び出すことがサガcall効果 - を反応させ使用する必要があります。あなたの場合、を実行して、約束を使用したくないときにコールバックと一緒に使用することを意図しています。ここで

const response = yield call(req) 

あなたがたSuperAgentに約束を操作する方法の詳細については見つけることができます: http://visionmedia.github.io/superagent/#promise-and-generator-support

を、あなたのコールラインからエンドを削除した場合は、要求が行われ、応答を取得する必要があります表示されるはずです
関連する問題