简体   繁体   English

未捕获的错误:动作必须是普通对象?

[英]Uncaught Error: Actions must be plain objects?

Uncaught Error: Actions must be plain objects. 未捕获的错误:动作必须是普通对象。 Use custom middleware for async actions..... 使用自定义中间件进行异步操作.....

Action file: 动作文件:

import $ from 'jquery'

import { phoneVerify } from '../actions/types'

const verifyPhoneAsync = function (verification) {
  return {
    type: phoneVerify,
    payload: verification
  }
}
const verifyPhone = function (phone) {
  $.ajax({
    url: 'api',
    type: 'POST',
    data: { mobile: phone },
    dataType: 'json',
    success: (data) => {
      console.log(data)
    }
  })
}
const verifyOtp = function (phone, otp) {
  return (dispatch) => {
    $.ajax({
      url: 'api',
      type: 'POST',
      data: { mobile: phone, code: otp, devicetoken: 'nil', devicetype: 'nil' },
      dataType: 'json',
      success: (data) => {
        if (data.success === true) {
          localStorage.setItem('MobileNumber', phone)
          const varification = data
          dispatch(verifyPhoneAsync(varification))
        } else {
          console.log('rfg')
          const varification = data
          dispatch(verifyPhoneAsync(varification))
        }
      }
    })
  }
}
export { verifyPhone, verifyOtp }

您忘记了返回验证电话,因此收到此错误。

Your store is not configured with the redux-thunk middleware. 您的商店未配置Redux级别的中间件。 Therefore your store doesn't know how to dispatch actions which are functions. 因此,您的商店不知道如何调度作为功能的动作。

Middleware is not baked into createStore and is not a fundamental part of the Redux architecture 中间件没有引入createStore中,也不是Redux架构的基本组成部分

Since you have not added any middleware you can only dispatch actions which are objects. 由于尚未添加任何中间件,因此只能分派作为对象的操作。

Firstly, you do not need compose when you only have one store enhancer. 首先,当您只有一个商店增强器时,您不需要撰写

Compose is used when we want to apply multiple store enhancers to our store. 当我们要将多个商店增强程序应用于商店时,将使用Compose。

applyMiddleware is a store enhancer for adding middleware to our dispatch chain. applyMiddleware是一种商店增强程序,用于将中间件添加到我们的调度链中。

Note that applyMiddleware and the only store enhancer built into redux 请注意applyMiddleware和redux中唯一内置的商店增强器

Store enhancers: extends store itself 商店增强器:扩展商店本身

Store enhancers are just higher order functions that give us new store creators with extended functionality. 商店增强器只是高阶功能,它使我们的新商店创建者拥有扩展的功能。

Middleware: extends dispatch method of store 中间件:扩展商店的调度方法

Whereas middleware are higher order functions that return a new dispatch method on our redux store that will run some custom logic when a new action is dispatched. 中间件是高阶函数,它们在我们的redux存储上返回新的调度方法,当调度新动作时,该方法将运行一些自定义逻辑。

How to set up a redux store with middleware such as redux-thunk 如何使用中间件(例如redux-thunk)设置redux存储

import { createStore, applyMiddleware } from 'redux'

let middleware = [ thunk ] // add additional middleware to this array

const store = createStore(
  reducer,
  preloadedState,
  applyMiddleware(...middleware)
)

So your code will look like this: 因此,您的代码将如下所示:

import React from 'react'
import ReactDOM from 'react-dom'
import { Provider } from 'react-redux'
import { Router, Route, browserHistory } from 'react-router'
import { createStore, applyMiddleware } from 'redux'
import thunk from 'redux-thunk'
import AppContainer from './views/App/container'
import './index.scss'
import reducer from './store/reducers'

let middleware = [ thunk ]

const store = createStore(
  reducer,
   applyMiddleware(...middleware)
)

ReactDOM.render(
   <Provider store={store}>
       <Router history={ browserHistory }>
         <Route path="/" component={ AppContainer }/>
       </Router>
   </Provider>,
   document.getElementById('root')
)

See the docs for the complete applyMiddleware api 有关完整的applyMiddleware API,请参阅文档

You have got this error because you are using an ajax call (asynchronous call) directly in your action , 之所以出现此错误,是因为您在action直接使用了ajax调用(异步调用),

that is because Actions are meant to be plain JavaScript objects and must have a type property that indicates the type of action being performed. 这是因为Actions是纯JavaScript对象,并且必须具有type属性,该属性指示要执行的操作的类型。

Consider using redux-thunk middleware for api call purposes to avoid this issue. 考虑将redux-thunk中间件用于api call目的,以避免出现此问题。

For more information: 欲获得更多信息:

Redux Async Actions Redux异步操作

Redux Thunk Redux Thunk

暂无
暂无

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

相关问题 未捕获的错误:操作必须是普通对象 - Uncaught Error: Actions must be plain objects 未捕获的错误:操作必须是普通对象。 使用自定义中间件 - Uncaught Error: Actions must be plain objects. Use custom middleware 未捕获的错误:操作必须是普通对象(React/Redux) - Uncaught Error: Actions must be plain objects (React/Redux) REACT-REDUX:未捕获错误:操作必须是普通对象 - REACT-REDUX : Uncaught Error: Actions must be plain objects 成功并行执行Multipe ajax调用并调度redux操作会导致“未捕获的错误:操作必须是普通对象” - Multipe ajax calls in parallel and dispatching redux actions after success leads to “Uncaught Error: Actions must be plain objects” componentwillmount() 未捕获错误:操作必须是普通对象。 使用自定义中间件进行异步操作 - componentwillmount() Uncaught Error: Actions must be plain objects. Use custom middleware for async actions Actions 必须是普通对象 Error In React 和 Redux - Actions must be plain objects Error In React and Redux Redux Actions 上的错误必须是普通对象 - Error on Redux Actions must be plain objects 显示未捕获的错误:在尝试调度数据以存储在 redux 中时,操作必须是普通对象 - Showing Uncaught Error: Actions must be plain objects, when trying to dispatch data to store in redux Error get&#39;Uncaught Error:操作必须是纯对象。 使用自定义中间件执行异步操作。” 由redux-thunk - Error get 'Uncaught Error: Actions must be plain objects. Use custom middleware for async actions.' by redux-thunk
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM