2016-08-16 47 views
1

我實際上試圖使用redux-promise,並且當我在我的操作中向我的api發送請求時,出現此錯誤。redux-promise錯誤:操作必須是普通對象。使用自定義中間件

enter image description here

我看到有一個演講受到同樣的錯誤,但我沒有找到我的問題的反應解決。

所以這是我的代碼:

./app.js

import React from 'react' 
import ReactDOM from 'react-dom' 
import { Provider } from 'react-redux' 
import { createStore, applyMiddleware } from 'redux' 
import { Router, browserHistory } from 'react-router' 
import reducers from './reducers/app-reducer' 
import routes from './routes' 

import promise from 'redux-promise' 

const createStoreWithMiddleware = applyMiddleware(promise)(createStore); 


ReactDOM.render(<Provider store={createStoreWithMiddleware(reducers)}> 
       <Router history={browserHistory} routes={routes} /> 
       </Provider>, document.querySelector('.container')) 

./reducers/app-reducer.js

import { combineReducers } from 'redux' 
import user from './user-reducer' 


const rootReducer = combineReducers({ 
    user: user 
}) 

export default rootReducer 

行動,其名爲:

./actions/user-actions.js

import axios from 'axios' 

export const UPDATE_TOKEN = 'UPDATE_TOKEN' 
export const CREATE_SESSION = 'CREATE_SESSION' 
export const CREATE_SESSION_ERROR = 'CREATE_SESSION_ERROR' 

export function createSession(obj){ 

    if (typeof obj === 'string') { 

     return { 
      type: UPDATE_TOKEN, 
      payload: obj 
     } 
    } 
    else { 
     axios.post('http://localhost:8080' + '/session', { 
      data: {'Email': obj.email, 'Password': obj.password}, 
      headers: {'Content-Type': 'application/json'} 
     }).then((response) => { 
      return { 
       type: CREATE_SESSION, 
       payload: response.data 
      } 
     }).catch((error) => { 
      return { 
       type: CREATE_SESSION_ERROR, 
       payload: error 
      } 
     }) 
    } 
} 

我也試圖與終極版-的thunk,我得到了同樣的錯誤。

有人你有想法嗎?或者也許我誤會了我? 謝謝

回答

1

在action中創建ajax調用時,應該使用redux-thunk和compose。

import {createStore, applyMiddleware, compose} from "redux"; 
import thunk from 'redux-thunk'; 

改變你的店是這樣的:

const createStoreWithMiddleware = compose(applyMiddleware(thunk))(createStore)(reducers); 

並設置Axios公司使用派遣:發佈我的問題後

return (dispatch) => { 
    axios.post('http://localhost:8080' + '/session', { 
     data: {'Email': obj.email, 'Password': obj.password}, 
     headers: {'Content-Type': 'application/json'} 
    }).then((response) => { 
     dispatch ({ 
     type: CREATE_SESSION, 
     payload: response.data 
     }) 
    }).catch((error) => { 
     return { 
     type: CREATE_SESSION_ERROR, 
     payload: error 
     } 
    }) 
} 
+1

是的,我已經找到了解決辦法很短的時間,但感謝你爲能見面的人發佈答案;) – Fantasim

相關問題