2017-05-19 9 views
0

で構成し使用していた場合でも:未知のエラー:アクションはプレーンなオブジェクトでなければなりません。非同期アクションにはカスタムミドルウェアを使用します。私はReduxのでAPI呼び出しからいくつかのデータを取得しようとしていますReduxの

import {createStore,combineReducers,compose,applyMiddleware} from "redux" 
import math from "./reducers/mathReducer" 
import user from "./reducers/userReducer" 
import thunk from "redux-thunk" 

const store = createStore(
     combineReducers({ 
       user,math 
      }), 
     {}, 
     compose(applyMiddleware(thunk)) 
     ); 
export default store; 

データコードをフェッチです:

export function setName() 
{ 
    fetch('http://localhost/login.php', { 
     method: 'POST', 
     headers: { 
      'Accept': 'application/json', 
     }, 
    }) 
    .then(response => response.json()) 
    .then((response) => { 
     return { 
      type:'SET_NAME', 
      payload: response.name 
     }; 
    }) 
    .catch((error) => { 
     console.warn('Actions - fetchJobs - recreived error: ', error) 
    }) 


} 

私は問題を取得しています:

Uncaught Error: Actions must be plain objects. Use custom middleware for async actions. 

私は間違っていますか?アクションがディスパッチを使用するように変更

+0

'applyMiddleware'はあなたが' compose'を必要としない使用している唯一のエンハンサーである場合。また、初期状態として空のオブジェクトは必要ありません。 'CONST店舗= CREATESTORE( \t \t combineReducers({ \t \t \t \tユーザ、数学 \t;ちょうど' CREATESTORE(combineReducers({ユーザー、数学})、applyMiddleware(サンク))は 'このような十分 –

+0

何かであろう\t \t})、 \t \t applyミッドウェア(サンク) \t \t);しかし、動作しません。 – learner

答えて

0

は、上記の問題を解決:

export function setName(name) 
{ 
    console.log("Inside setName."); 
    return dispatch => 
    { 
     fetch('http://localhost/login.php', { 
      method: 'POST', 
      headers: { 
       'Accept': 'application/json', 
      }, 
     }) 
     .then(response => response.json()) 
     .then((response) => { 
      dispatch({ 
       type:'SET_NAME', 
       payload: response.name 
      }); 

     }) 
     .catch((error) => { 
      console.warn('Actions - fetchJobs - recreived error: ', error) 
     }) 

    } 
} 
関連する問題

 関連する問題