2017-06-12 41 views
0

我需要向我的API端點發送上傳文件的請求。我在我的項目中使用axios,但它看起來像是一個附加文件的問題,而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 - 效應調用一些返回一個承諾。在你的情況下,你要求它運行的最終功能,當你不想使用promise時,這個功能打算與回調一起使用。如果您從您的電話線結束時,你應該看到被提出的要求和應該得到的迴應:

const response = yield call(req) 

在這裏你可以找到更多有關如何使用承諾在SuperAgent的工作: http://visionmedia.github.io/superagent/#promise-and-generator-support

相關問題