javascript - Login.js:6 Uncaught TypeError: Cannot read property 'then' of undefined
问题描述
登录成功后,我试图重定向到仪表板,但它不起作用。我收到类似这样的错误。
未捕获的类型错误:无法读取未定义的属性“then”
这是我的代码
登录.js
import {connect} from 'react-redux';
import LoginForm from "../Form/LoginForm";
import {userLoggedIn} from '../../redux/actions/auth';
class Login extends Component {
submit = data => this.props.userLoggedIn(data).then(()=> this.props.history.push("/dashboard"));
render() {
console.log(this.props) <!---here when i try to log see image for result---->
return (
<div>
<LoginForm submit={this.submit}/>
</div>
);
}
}
export default connect(null, {userLoggedIn})(Login);
redux/action/auth.js
export const userLoggedIn = (user)=> dispatch => {
axios.post('/api/login', {user})
.then(res => {
localStorage.redditToken = res.data.user.token;
setAuthorizationHeader(res.data.user.token);
dispatch({
type: USER_LOGGED_IN,
payload: res
})
})
.catch()
};
解决方案
您没有返回这axios call
就是为什么它显示为空。正如上面评论中所说,一个承诺应该恢复then
工作。只需返回axios.post
.
export const userLoggedIn = (user) => dispatch => {
return axios.post('/api/login', {
user
})
.then(res => {
localStorage.redditToken = res.data.user.token;
setAuthorizationHeader(res.data.user.token);
dispatch({
type: USER_LOGGED_IN,
payload: res
})
})
.catch()
};
推荐阅读
- java - 在 Java 项目中连接到服务器时出现问题
- azure - 用于 Azure Web 监控服务的 Azure python sdk
- data-structures - 数据结构与算法
- hanami - Hamani:实体和存储库有什么区别?
- bash - 是否有脚本逻辑我可以只保留文件的前两行并注释掉其余的行?
- flutter - Flutter ElevatedButton 自定义样式
- javascript - 如何对饼图数据进行百分位计算?
- python - 为 Python 安装 frePPLe 库
- graphdb - 如何在 GraphDB 最新免费版本中编辑存储库 URL?
- codeigniter - Codeigniter 4 - 如何自定义下载文件的文件名(具有不同的文件扩展名,如 jpg、jpeg / pdf)?