每次用户成功注册后,我总是会收到错误消息
未处理的拒绝(TypeError):无法读取的属性“数据” 未定义
调度({ 类型:GET_ERRORS, 有效负载:err.response.data })
如果用户成功注册后他们没有错误,为什么要执行此catch方法?
console.log(err.response)显示用户何时使用相同的用户名或电子邮件进行注册。
我做错了什么?
authActions.js
export const registerUser = (userData) => dispatch => {
Axios
.post('/users/register', userData)
.then( res => {
const token = res.data.token;
console.log(token);
// pass the token in session
sessionStorage.setItem("jwtToken", token);
// set the auth token
setAuthToken(token);
// decode the auth token
const decoded = jwt_decode(token);
// pass the decoded token
dispatch(setCurrentUser(decoded))
this.props.history.push("/dashboard")
}).catch( (err) => {
console.log(err.response)
dispatch({
type: GET_ERRORS,
payload: err.response.data
})
})
};
减速器
import {SET_CURRENT_USER } from '../actions/types';
import isEmpty from '../actions/utils/isEmpty';
const initialState = {
isAuthenticated: false
}
export default (state = initialState, action) => {
switch (action.type) {
case SET_CURRENT_USER:
return{
...state,
isAuthenticated: !isEmpty(action.payload),
user:action.payload
}
default:
return state;
}
}
console.log(res)
{
"data": {
"message": "user created",
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6NDEsImlhdCI6MTU1OTI2ODcxNX0.t7xK5VVdOmj2BbExBmOdUrZHwYuyEJgjvUTQq1Mw5qY",
"auth": true
},
"status": 200,
"statusText": "OK",
"headers": {
"content-type": "application/json; charset=utf-8",
"content-length": "165"
},
"config": {
"transformRequest": {},
"transformResponse": {},
"timeout": 0,
"xsrfCookieName": "XSRF-TOKEN",
"xsrfHeaderName": "X-XSRF-TOKEN",
"maxContentLength": -1,
"headers": {
"Accept": "application/json",
"Content-Type": "application/json"
},
"method": "post",
"baseURL": "http://localhost:3000",
"withCredentials": true,
"url": "http://localhost:3000/users/register",
"data": "{\"username\":\"billyssss999\",\"email\":\"j0hnnnysssraddddddin@yahoo.com\",\"password\":\"janeddmdddba\"}"
},
"request": {}
答案 0 :(得分:0)
因此,在我删除此内容后
this.props.history.push("/dashboard")
,并已通过此现有代码重定向到仪表板。
注册
....
componentDidMount() {
// console.log(this.props.auth);
if (this.props.auth.isAuthenticated) {
this.props.history.push("/dashboard");
}
}
componentWillReceiveProps(nextProps) {
if (nextProps.auth.isAuthenticated) {
this.props.history.push("/dashboard");
}
if (nextProps.errors) {
this.setState({ errors: nextProps.errors });
}
}
handleChange = (e) => {
e.preventDefault();
const {formData} = this.state;
this.setState({
formData: {
...formData,
[e.target.name]: e.target.value
}
});
}
handleSubmit = (e) => {
e.preventDefault();
const {formData} = this.state;
const {username, email, password, passwordConf} = formData;
this.setState({
username: this.state.username,
password: this.state.password,
passwordConf: this.state.passwordConf,
email: this.state.email
});
const creds = {
username,
email,
password
}
console.log(creds);
if (password === passwordConf) {
this.props.registerUser(creds, this.props.history);
} else {
this.setState({passErr: "Passwords Don't Match"})
}
}