【发布时间】:2018-03-29 14:09:29
【问题描述】:
我有以下代码,它抛出的错误必须是普通对象。我认为问题出在 redux thunk 但我无法纠正它。
动作文件-
import axios from "axios";
export const addBiller = (biller = {}) => {
return async dispatch => {
const res = await axios.post("/api/biller", biller);
dispatch({ type: "ADD_BILLER", payload: res.data });
};
};
我的商店-
import { createStore, combineReducers, applyMiddleware } from "redux";
import billerReducer from "../reducers/Billers";
import userReducer from "../reducers/User";
import billReducer from "../reducers/Bills";
import authReducer from "../reducers/Auth";
import reduxThunk from "redux-thunk";
export default () => {
const store = createStore(
combineReducers(
{
Billers: billerReducer,
Users: userReducer,
Bills: billReducer,
Auth: authReducer
},
applyMiddleware(reduxThunk)
)
);
return store;
};
后端文件-
const mongoose = require("mongoose");
const Biller = mongoose.model("biller");
module.exports = app => {
app.post("/api/biller", async (req, res) => {
const { billerName, billerDescription } = req.body;
const biller = new Biller({
billerName: billerName,
billerDescription: billerDescription
});
biller = await biller.save();
res.send(biller);
});
};
我的减速机-
const BillerDefaultState = [];
const billerReducer = (state = BillerDefaultState, action) => {
switch (action.type) {
case "ADD_BILLER":
return [...state, action.biller];
default:
return state;
}
};
export default billerReducer;
【问题讨论】:
-
您的 createStore 方法错误。完成 combineReducers 函数,然后将中间件作为第三个参数传递给 createStore 方法。
-
createStore 的第二个参数是预加载状态。 vampurrmaid 的解决方案应该可以帮助您。
标签: reactjs redux axios middleware redux-thunk