2017-08-19 7 views
2

私はReact、Redux、およびMongoDBを使用してほとんどアプリケーションを作成していません。 残念ながら私は腋窩reduxを使用することに問題があります。私はそれをこのように減らしてみました:reduxでAxiosを使用する適切な方法

export function users(state = initialState, action) { 
    switch (action.type) { 

    case 'USER_ADD': 
     { 
     axios.post('http://localhost:4200/users/add/post', {user: 
     action.user}).then(() => { 
      return { 
      ...state, 
      items: [ 
       ...state.users, 
       action.user 
      ] 
      } 
     }).catch(err => console.log(err)); 
     break; 
     } 
    ......... 

しかし、それは動作しません。それから私は私の行動のクリエイターにaxiosを移動し、それは次のようになります。それは、モンゴデータベースに新しい文書をポストが、それはまた、私にエラーを与える

export function addUser(user) { 

    return function (dispatch) { 
    axios.post('http://localhost:4200/users/add/user', {user:user}) 
     .then(() => dispatch({ 
     type: USER_ADD, 
     user: user 
     })).catch(err => console.log(err)); 
    } 
} 

:アクションは、プレーンなオブジェクトでなければなりません。非同期アクションにはカスタムミドルウェアを使用します。はい、私はreduxサンクを使用しています;)

誰が問題をどこに教えていただけますか?他のコードが役に立つかどうかは分かりませんが、もっとコードを求めてください。

EDIT:

私はそのようにReduxの-サンクを使用しています:

import { Provider } from 'react-redux'; 
import { createStore, applyMiddleware } from 'redux'; 
import thunkMiddleware from 'redux-thunk'; 
import reducers from './reducers'; 


const createStoreWithMiddleware = applyMiddleware(thunkMiddleware) 
(createStore); 

ReactDOM.render(
    <Provider store={createStoreWithMiddleware(reducers)}> 
    <App /> 
    </Provider>, 
    document.getElementById('root') 
); 
+0

redux-thunkをインポートしましたか? – WitVault

+0

はい、どのように私が行ったかを示すために私の投稿を編集しています – Kreha

+0

サイドノート:リダクターは副作用のない純粋な機能でなければなりません。 –

答えて

1

コードの下にしてみてください。私はあなたが店を正しく作っていないと思う。

import { Provider } from 'react-redux'; 
import { createStore,combineReducers, applyMiddleware } from 'redux'; 
import thunkMiddleware from 'redux-thunk'; 
import reducers from './reducers'; 

let reducer = combineReducers(reducers) 
// applyMiddleware supercharges createStore with middleware: 
const createStoreWithMiddleware = createStore(reducer, applyMiddleware(thunkMiddleware)) 
ReactDOM.render(
    <Provider store={createStoreWithMiddleware}> 
    <App /> 
    </Provider>, 
    document.getElementById('root') 
); 
+0

私は既にcombineReducersを使用しました。私が './reducers'からインポートしたレデューサーは、一つにまとめられたレデューサーです。もう一度結合する必要がありますか? – Kreha

+0

@krehaいいえ、あなたはそれを必要としません。 – WitVault

+0

まだこのエラーがあります – Kreha

関連する問題