简体   繁体   English

未捕获的错误:操作必须是普通对象。 使用自定义中间件进行异步操作。 即使我在redux中使用compose

[英]Uncaught Error: Actions must be plain objects. Use custom middleware for async actions. even if I am using compose in redux

I am trying to get some data from api call in redux: 我试图从redux中的api调用中获取一些数据:

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;

The fetch data code is: 获取数据代码是:

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)
    })


}

I am getting issue: 我遇到了问题:

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

What am I doing wrong? 我究竟做错了什么?

Changing actions to use dispatch solved the above issue: 更改使用调度的操作解决了上述问题:

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)
        })

    }   
}

暂无
暂无

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

相关问题 Redux返回错误'操作必须是普通对象。 使用自定义中间件进行异步操作。 即使中间件已定义 - Redux returns error 'Actions must be plain objects. Use custom middleware for async actions.' even though middleware is defined 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-redux错误 - Error: Actions must be plain objects. Use custom middleware for async actions. React-redux error Redux 给出错误“错误:操作必须是普通对象。 使用自定义中间件进行异步操作。” - Redux gives error “Error: Actions must be plain objects. Use custom middleware for async actions.” “动作必须是简单的对象。 使用自定义中间件进行异步操作。”同时使用redux-thunk - “Actions must be plain objects. Use custom middleware for async actions.” while using redux-thunk 为什么我会收到错误:操作必须是普通对象。 使用自定义中间件进行异步操作。 - Why to do I get error: Actions must be plain objects. Use custom middleware for async actions.? 错误:操作必须是普通对象。 使用自定义中间件进行异步操作。 我究竟做错了什么? - Error: Actions must be plain objects. Use custom middleware for async actions. What am I doing wrong? 动作必须是普通对象。 使用自定义中间件进行异步操作,但我使用的是redux-thunk - Actions must be plain objects. Use custom middleware for async actions, but I am using redux-thunk 动作必须是普通对象。 使用自定义中间件进行异步操作。 redux-thunk相关 - Actions must be plain objects. Use custom middleware for async actions. redux-thunk related 动作必须是普通对象。 使用自定义中间件进行异步操作。 React,Redux,Thunk - Actions must be plain objects. Use custom middleware for async actions. React, Redux, Thunk
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM