简体   繁体   English

未捕获的错误:操作必须是普通对象。 使用自定义中间件进行异步操作。 - Laravel-react 项目

[英]Uncaught Error: Actions must be plain objects. Use custom middleware for async actions. - Laravel-react project

Firstly, I am using react within a Laravel project and trying to list packages within Package.js component through an async call using axios through the use of an action creator.首先,我在 Laravel 项目中使用 react 并尝试通过使用 axios 的异步调用通过 action creator 列出 Package.js 组件中的包。 Even though I have used redux-thunk middleware, it throws an error 'Uncaught Error: Actions must be plain objects.即使我使用了 redux-thunk 中间件,它也会抛出错误“未捕获的错误:操作必须是普通对象。 Use custom middleware for async actions.'.使用自定义中间件进行异步操作。'。 below is my code and you can even access my code in GitHub - [https://github.com/sunethice/Emerald_Tours].下面是我的代码,您甚至可以在 GitHub 中访问我的代码 - [https://github.com/sunethice/Emerald_Tours]。

index.js索引.js

 import React, {Component} from 'react'; import ReactDOM from 'react-dom'; import { Provider } from 'react-redux'; import { persistStore } from 'redux-persist'; import { PersistGate } from 'redux-persist/lib/integration/react'; import { configureStore } from './store'; import {BrowserRouter as Router, Link, Route, Switch} from 'react-router-dom'; import './css/index.css'; import Customer from './Customer'; import Admin from './Admin'; class Index extends Component { render() { return ( <Router> <Route exact path="/" component={Customer}></Route> <Route exact path="/admin" component={Admin}></Route> </Router> ); } } const store = configureStore(); const persistor = persistStore(store); ReactDOM.render( <Provider store={store}> <PersistGate loading="<div>Loading...</div>" persistor={persistor}> <Index/> </PersistGate> </Provider>, document.getElementById('root'));

store.js商店.js

 import { createStore, combineReducers, applyMiddleware } from 'redux'; import { packages } from './components/reducers'; import { persistReducer } from 'redux-persist'; import storage from 'redux-persist/lib/storage'; import autoMergeLevel2 from 'redux-persist/lib/stateReconciler/autoMergeLevel2'; import thunk from 'redux-thunk'; import { composeWithDevTools } from 'redux-devtools-extension'; const reducers = { packages, }; const persistConfig = { key: 'root', storage, stateReconciler: autoMergeLevel2, } const rootReducer = combineReducers(reducers); const persistedReducer = persistReducer(persistConfig, rootReducer); export const configureStore = () => createStore( persistedReducer, {}, composeWithDevTools( applyMiddleware(thunk) ) );

reducers.js减速器.js

 import { LIST_PACKAGES, LIST_PACKAGES_FAILURE } from './actionTypes'; export const packages = (state = [], action) => { const { type, payload } = action; switch(type){ case LIST_PACKAGES: const newList = { packages: payload }; return state.concat(newList); case LIST_PACKAGES_FAILURE: return state; default: return state; } }

actions.js动作.js

 import axios from 'axios'; import { LIST_PACKAGES, LIST_PACKAGES_FAILURE } from './actionTypes'; export function listPackages () { return (dispatch) => { axios.get('/api/packages') .then((res) =>{ dispatch({type: LIST_PACKAGES ,payload:res.data}); }) .catch((error)=> { dispatch({type: LIST_PACKAGES_FAILURE, payload: error}); }) } }

Package.js包.js

 import React,{ Component, useState } from "react"; import { connect } from 'react-redux'; import { listPackages } from '../actions'; import pkg1 from '../../img/tour-package1.jpg'; class Package extends Component { componentDidMount(){ this.props.fetchData(); } render() { return ( <div> <div className="container mt-5"> <div className="row"> <div className="col-12"> <div className="h3 text-center"> Tour Packages </div> <p> Lorem ipsum dolor sit amet, curabitur nec lacus pellentesque ut facilisis, lacus iaculis turpis interdum pede, sapien quis amet vitae, erat parturient, turpis congue sit. Hac nulla phasellus ornare. Volutpat risus ipsum nulla ducimus erat. Scelerisque eu imperdiet wisi wisi, sit libero sed ipsum sodales phasellus, odio vel ac non ac, sodales viverra, metus volutpat quis rutrum diam ac integer. Posuere nullam eu vestibulum non nonummy. Metus purus ac malesuada vitae ut qui, sed rhoncus nonummy, massa ac urna risus, faucibus aliquam malesuada fusce gravida urna. </p> </div> </div> <div className="row"> <div className="col-md-12 d-flex justify-content-center mb-5"> <div className="btn-group"> <button type="button" className="btn btn-outline-secondary" data-rel="0" > Sri Lanka </button> <button type="button" className="btn btn-outline-secondary" data-rel="1" > Maldives </button> </div> </div> </div> <div className="row"> <div className="col-md-4"> <div className="travel-place"> <div className="work-image"> <img src={pkg1} className="img-fluid person" alt="destination" /> <div className="overlay"> <div className="overlay_shape"> <a href="#" className="over-btn"> 10 photos </a> <a href="tourpackage.html" className="view-all" > View All Places <i className="fas fa-long-arrow-alt-right"></i> </a> </div> </div> </div> <div className="travel-text"> <h3>Love City, Paris, Italy</h3> <p> for 7Days <span>|</span> $ 710.00 </p> </div> </div> </div> </div> </div> </div> ); } } const mapStateToProps = (state) => ({ packages : state.packages }); const mapDispatchToProps = (dispatch) => ({ fetchData: () => dispatch(listPackages()) }); export default connect(mapStateToProps, mapDispatchToProps )(Package);

Much appreciate it if you can let me know where I am doing wrong.如果您能让我知道我做错了什么,将不胜感激。

I figured it out.我想到了。 It was nothing to do with the code.这与代码无关。 It's something to do with the dependencies.这与依赖关系有关。 I just typed 'npm outdated' to check the outdated dependecies and updated them.我只是输入“npm outdated”来检查过时的依赖关系并更新它们。 Then everything worked fine.然后一切正常。

暂无
暂无

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

相关问题 错误:动作必须是普通对象。 使用自定义中间件进行异步操作。 React-redux错误 - Error: Actions must be plain objects. Use custom middleware for async actions. React-redux error 错误:操作必须是普通对象。 使用自定义中间件进行异步操作。 在 React Native 中 - Error: Actions must be plain objects. Use custom middleware for async actions. in React Native 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 动作必须是普通对象。 使用自定义中间件进行异步操作。 React,Redux,Thunk - Actions must be plain objects. Use custom middleware for async actions. React, Redux, Thunk 动作必须是普通对象。 使用自定义中间件进行异步操作。 react-redux - Actions must be plain objects. Use custom middleware for async actions. react-redux Redux 给出错误“错误:操作必须是普通对象。 使用自定义中间件进行异步操作。” - Redux gives error “Error: Actions must be plain objects. Use custom middleware for async actions.” 未捕获的错误:操作必须是普通对象。 使用自定义中间件进行异步操作。 即使我在redux中使用compose - Uncaught Error: Actions must be plain objects. Use custom middleware for async actions. even if I am using compose in redux “行动必须是普通的对象。 使用自定义中间件进行异步操作。“with react / redux - “Actions must be plain objects. Use custom middleware for async actions.” with react/redux 错误:操作必须是普通对象。 使用自定义中间件进行异步操作。 如何解决? - Error: Actions must be plain objects. Use custom middleware for async actions. How to fix it? 如何修复:错误:操作必须是普通对象。 使用自定义中间件进行异步操作。? - How to fix: Error: Actions must be plain objects. Use custom middleware for async actions.?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM