2017-07-21 24 views
1

私は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の形式を使用しています)フォームを送信するとき、私は私のコンソールログに取得なぜ、これは次のとおりです。

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

とさえauthSuccessアクションは、空のペイロード(result

私はここで何か間違ったことをやっていると派遣なっていますか?

答えて

1

あなたはreturnが欠落している:

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

あなたははい、これは私が欠けていたもので、私はこれをデバッグ過ごした時間を信じられないでしょう。ありがとう – Soufiaane

関連する問題