' Redux的-佐贺'不是异步工作

时间:2017-07-21 21:07:12

标签: javascript reactjs ecmascript-6 axios redux-saga

我尝试(学习)使用redux-saga发出异步发布请求,但我得到了同步行为。

这是我使用的代码:

import {AUTH_REQUEST} from '../constants/authentication';
import {authSuccess, authError} from '../actions/login';
import {takeEvery, call, put, fork, all} from 'redux-saga/effects';
import axios from 'axios';

const authenticate = (username, password) => {
    axios
        .post('http://localhost:8000/api/auth/login/', {username, password})
        .then((response) => {
            console.log('RESPONSE: ', response.data);
            return response.data;
        })
        .catch((error) => {
            throw error;
        });
};

function* watchAuthRequest({username, password, resolve, reject}) {
    try {
        const result = yield call(authenticate, username, password);
        console.log('RESULT', result);
        yield put(authSuccess(result));
        yield call(resolve);
    } catch (error) {
        yield put(authError(error));
        yield call(reject, {serverError: 'Something bad happend !'});
    }
}

const authSaga = function* authSaga() {
    yield takeEvery(AUTH_REQUEST, watchAuthRequest);
};

export default function* rootSaga() {
    yield all([
        fork(authSaga),
    ]);
};

当我提交表单时(我使用redux-form),这就是我进入我的控制台日志的原因:

RESULT: undefined
RESPONSE:  Object {user: Object, token: "04a06266803c826ac3af3ffb65e0762ce909b07b2373c83b5a25f24611675e00"}

甚至使用空的有效负载(authSuccess)调度result操作

我在这里做错了吗?

1 个答案:

答案 0 :(得分:2)

您错过了i = r.randrange(len(cpureturn)) # get an index, not a value del cpureturn[i]

return