Javascript 错误服务器错误:操作必须是普通对象。使用自定义中间件进行异步操作
我正在尝试编写React web应用程序,但收到一个错误。我不明白为什么它不起作用。 当用户进入我的web应用程序时,我想将他的电子邮件地址保存在Redux商店中 types.jsJavascript 错误服务器错误:操作必须是普通对象。使用自定义中间件进行异步操作,javascript,reactjs,asynchronous,redux,react-redux,Javascript,Reactjs,Asynchronous,Redux,React Redux,我正在尝试编写React web应用程序,但收到一个错误。我不明白为什么它不起作用。 当用户进入我的web应用程序时,我想将他的电子邮件地址保存在Redux商店中 types.js export const LOGIN = 'LOGIN'; export const DATA = 'DATA'; 在这里,我定义了我的行为类型 searchActions.js import {LOGIN, DATA} from './types'; import axios from 'axios'; impo
export const LOGIN = 'LOGIN';
export const DATA = 'DATA';
在这里,我定义了我的行为类型
searchActions.js
import {LOGIN, DATA} from './types';
import axios from 'axios';
import store from '../store'
export const login = ()=> {
store.dispatch({
type: LOGIN
})
};
export const information = async(email) => {
store.dispatch({
type: DATA,
payload: email
})
}
这里写下了我的行为必须根据类型做的事情
userReducer.js
import {LOGIN, DATA} from '../actions/types';
const initialState = {
isLogged: false,
email: '',
}
export default function(state = initialState, action){
switch(action.type){
case LOGIN:
return {
...state,
isLogged: true
};
case DATA:
return {
...state,
email: action.payload
};
default:
return state;
}
}
这是一个简单的形式。如果数据库的回复是肯定的,我想把他的邮件保存在商店里。如果删除第41行,效果会很好。
SignIn.jsx
import React, { Component } from 'react';
import { connect } from 'react-redux';
import {login, information} from '../actions/searchActions';
import './component.css'
import axios from 'axios';
const baseUrl = "http://localhost:3000";
class SignIn extends Component {
state = {
email:'',
password: '',
}
render() {
const handleChange = (e) => {
this.setState({[e.target.name]: e.target.value});
}
const handleSubmit = (e) => {
//aggiungere la validazione del form in una futura versione
e.preventDefault();
access();
}
const access = () => {
const url = baseUrl + '/router/sign-in';
axios.get(url, {
params: {
email: this.state.email,
password: this.state.password
}
})
.then(res=>{
if (res.data.status !== undefined) {
const data = res.data.status
console.log(JSON.stringify(data)) //users entrato
login();
this.props.history.push('/')
this.props.information(this.state.email) // HERE IS THE ERROR.
// if I delate this line it works
}
else {
alert(res.data.error)
}
})
.catch(error=>{
alert("Error server "+ error)
})
}
return (
<div className="firstly-square">
<div className="center-form">
<form action="" className="sign" onSubmit={handleSubmit}>
<div className="title-sign">
SignIn
</div>
<div className="signin-username">
<label htmlFor="email" className="signin-label">email</label>
<input type="text" name="email" className="signin-input" onChange={handleChange} value={this.state.email}/>
</div>
<div className="signin-password">
<label htmlFor="password" className="signin-label">password</label>
<input type="password" name="password" className="signin-input" onChange={handleChange}/>
</div>
<div className="signin-bottone">
<button type="submit" className="signin-button">Sumbit</button>
</div>
</form>
</div>
</div>
);
}
}
const mapStateToProps = state => ({
email : state.user.email
});
export default connect( mapStateToProps, { information })(SignIn);
import React,{Component}来自'React';
从'react redux'导入{connect};
从“../actions/searchActions”导入{login,information};
导入“./component.css”
从“axios”导入axios;
常量baseUrl=”http://localhost:3000";
类签名扩展组件{
状态={
电子邮件:“”,
密码:“”,
}
render(){
常数handleChange=(e)=>{
this.setState({[e.target.name]:e.target.value});
}
常量handleSubmit=(e)=>{
//在未来的版本中,格式为
e、 预防默认值();
access();
}
常量访问=()=>{
const url=baseUrl+'/router/sign-in';
获取(url{
参数:{
电子邮件:this.state.email,
密码:this.state.password
}
})
。然后(res=>{
if(res.data.status!==未定义){
const data=res.data.status
console.log(JSON.stringify(data))//用户
登录();
this.props.history.push(“/”)
this.props.information(this.state.email)//下面是错误。
//如果我钻研这条线,它就行了
}
否则{
警报(res.data.error)
}
})
.catch(错误=>{
警报(“错误服务器”+错误)
})
}
报税表(
签名
电子邮件
密码
萨姆比特
);
}
}
常量mapStateToProps=状态=>({
电子邮件:state.user.email
});
导出默认连接(mapstatetops,{information})(SignIn);
请帮帮我。
谢谢这是我的商店.js
import thunk from 'redux-thunk';
import {composeWithDevTools} from 'redux-devtools-extension';
import rootReducer from './reducers'
const middleware = [thunk];
const initialState = {};
const store = createStore(rootReducer, initialState, composeWithDevTools(applyMiddleware(...middleware)));
export default store;
您是否使用任何用于异步操作的中间件?检查是否需要在配置存储区中配置中间件redux thunk以使用dispatch