簡體   English   中英

Redux-Saga和Superagent

[英]Redux-Saga and Superagent

我需要向我的API端點發出請求以上傳文件。 我在整個項目中都使用了axios,但是將文件附加到其中似乎是個問題,而對於Superagent來說應該很簡單。 但是,我的Saga代碼無法與Superagent一起使用(無響應對象,未觸發API)我在做什么錯?

 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}) } } 

您需要使用react sagas call effect來調用返回promise的東西。 在您的情況下,您要讓它運行結束函數,該函數打算在不想使用promise時與回調一起使用。 如果您從通話線路中刪除該末端,則應該看到正在發出請求並應該得到響應:

const response = yield call(req)

在這里,您可以找到有關如何在超級代理中使用諾言的更多信息: http : //visionmedia.github.io/superagent/#promise-and-generator-support

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM