简体   繁体   English

componentwillmount() 未捕获错误:操作必须是普通对象。 使用自定义中间件进行异步操作

[英]componentwillmount() Uncaught Error: Actions must be plain objects. Use custom middleware for async actions

I'm implementing get all image by type with redux-saga.我正在使用 redux-saga 实现按类型获取所有图像。 I have 2 types, let's say, type kristik and type motif.我有 2 种类型,比如说,类型 kristik 和类型主题。 When I'm implementing type kristik, it got successful response, but when it comes to type motif, the response is error.当我实现类型 kristik 时,它得到了成功的响应,但是当涉及到类型主题时,响应是错误的。

here my code that has the error in console这里我的代码在控制台中有错误

    componentWillMount() => {
        const { dispatch } = this.props;

        dispatch(getAllMotif());
    }

I got error in dispatch(getAllMotif());我在dispatch(getAllMotif()); in commponentWillMount()commponentWillMount()

Here my getAllMotif() code这是我的getAllMotif()代码

    getAllMotif(token) {
        const path = `motif`;
        const method = 'get';

        return request.process(method, path, null, token);
    },

Here my sagas getAllMotif code这是我的sagas getAllMotif代码

export function* getAllMotif() {
    try {
        let { detail } = yield select(state => state.user);

        const result = yield call(API.getAllMotif, detail.api_token);
        yield put({
            type: types.GET_ALL_MOTIF_SUCCESS,
            payload: result,
        });
    } catch (err) {
        yield put(handleError(err));

        yield put({
            type: types.GET_ALL_MOTIF_FAILURE,
            payload: err,
        });
    }
}

here my reducer这是我的减速机

            case types.GET_ALL_MOTIF_SUCCESS:
            return {
                ...state,
                motif: [
                    ...action.payload.data.data
                ]
            };  

here my request code这是我的请求代码

internals.process = (method, path, payload, token, contentType=internals.contentType) => {
    const request = {
        url: `${API_URL}/${path}`,
        method: method,
        headers: {
            'Content-Type': contentType,
            'Accept': 'application/json',
        },
    };

    if (token) {
        request.params = {
            token: token,
        };
    }

    if (payload) {
        request.data = payload;
    }

    return axios.request(request)
        .then(res => {
            if (![200, 201].includes(res.status)) {
                throw new Error(res.status);
            }

            return res.data;
        })
        .catch((error) => {
            console.error(method, path, error);
            return Promise.reject({
                message: error.response.data.error,
                code: error.response.status
            });
        });
};

I don't know why in this type get error, because in type kristik also have very similar code.我不知道为什么在这种类型中会出错,因为在类型 kristik 中也有非常相似的代码。

You didn't dispatch an action that wasn't a plain object, your function getAllMotif not return a plain object.您没有发送不是普通 object 的操作,您的 function getAllMotif没有返回普通 object。 That lead to the error here.这导致了这里的错误。

You should dispatch an normal action你应该派发一个正常的动作

getAllMotifAction(token) {
    const path = `motif`;
    const method = 'get';

    return { type: 'GET_ALL_MOTIF', data: { path, method } };
},

Then in in saga, you catch this action and handle it with your saga function然后在 saga 中,你抓住这个动作并用你的 saga function 处理它

takeLatest('GET_ALL_MOTIF', getAllMotif);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的错误:操作必须是普通对象。 使用自定义中间件 - Uncaught Error: Actions must be plain objects. Use custom middleware Error get'Uncaught Error:操作必须是纯对象。 使用自定义中间件执行异步操作。” 由redux-thunk - Error get 'Uncaught Error: Actions must be plain objects. Use custom middleware for async actions.' by redux-thunk React - Native ' Redux Uncaught Error: Actions 必须是普通对象。 按下按钮时使用自定义中间件进行异步操作 - React - Native ' Redux Uncaught Error: Actions must be plain objects. Use custom middleware for async actions' on button press 异步操作 Redux 未处理拒绝(错误):操作必须是普通对象。 使用自定义中间件进行异步操作 - Async Action Redux Unhandled Rejection (Error): Actions must be plain objects. Use custom middleware for async actions 错误服务器错误:操作必须是普通对象。 使用自定义中间件进行异步操作 - Error server Error: Actions must be plain objects. Use custom middleware for async actions 错误:动作必须是普通对象。 使用自定义中间件进行异步操作。 React-redux错误 - Error: Actions must be plain objects. Use custom middleware for async actions. React-redux error React / Redux:错误:操作必须是普通对象。 使用自定义中间件进行异步操作 - React/Redux: Error: Actions must be plain objects. Use custom middleware for async actions 错误:动作必须是普通对象。 在删除按钮中将自定义中间件用于异步操作? - Error: Actions must be plain objects. Use custom middleware for async actions, in a delete button? Redux错误操作必须是普通对象。 使用自定义中间件进行异步操作 - Redux Error Actions must be plain objects. Use custom middleware for async actions 错误:操作必须是普通对象。 使用自定义中间件进行异步操作。 Redux 工具包 - Error: Actions must be plain objects. Use custom middleware for async actions. Redux toolkit
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM