[英]Unable to integrate by React frontend and Flask backend
I am trying to integrate my React frontend with Flask Backend.我正在尝试将我的 React 前端与 Flask 后端集成。 However I am unable to do so.
但是我无法这样做。 When I open my "Forum" page and try to login with the credentials Username: pranay_kothari and Password: 1234 (Haven't fixed the "Sign Up" page, so manually added this in my database) It gives an error message that I am unable to understand.
当我打开我的“论坛”页面并尝试使用凭据登录时 用户名:pranay_kothari 和密码:1234(尚未修复“注册”页面,因此在我的数据库中手动添加)它给出了一条错误消息,我是无法理解。
Here is the Error Message:这是错误消息:
Unhandled Rejection (TypeError): Cannot read property 'data' of undefined
(anonymous function)
C:/Users/Administrator/Desktop/Problems/src/components/forms/LoginForm.jsx:40
37 | this.setState({loading:true});
38 | this.props.submit(this.state.data)
39 | .catch(err => this.setState({
> 40 | errors: err.response.data.errors,
41 | errors: {global: false},
42 | loading:false
43 | }));
Login Form is as follows:登录表格如下:
import React, {Component} from 'react'
import {Form, Button, Message} from 'semantic-ui-react';
import InlineError from '../misc/InlineError';
import './LoginForm.css';
class LoginForm extends Component{
constructor(props){
super(props);
this.state={
data:{
"username":'',
"password":''
},
loading:false,
errors:{},
}
this.onChange=this.onChange.bind(this);
this.onSubmit=this.onSubmit.bind(this);
}
onChange = (e) =>{
this.setState({
data:{...this.state.data,[e.target.name]: e.target.value}
})
}
onSubmit = () =>{
const errors={};
if(!this.state.data.username){
errors.username= "username required";
}
if(!this.state.data.password){
errors.password="password required";
}
this.setState({
errors:errors
})
if(Object.keys(errors).length === 0){
this.setState({loading:true});
this.props.submit(this.state.data)
.catch(err => this.setState({
errors: err.response.data.errors,
errors: {global: false},
loading:false
}));
}
}
render(){
return(
<Form onSubmit={this.onSubmit} loading={this.state.loading} className = "Login-Form">
{this.state.errors.global && (
<Message negative>
<Message.Header>Something went wrong</Message.Header>
<p>{this.state.errors.global}</p>
</Message>
)}
<Form.Field error={!!this.state.errors.username}>
<label htmlFor="username" style = {{color: "white"}}>Username</label>
<input className="Login-Inputs"
type="username"
id="username"
name="username"
placeholder="username"
value={this.state.data.username}
onChange={this.onChange}
/>
{this.state.errors.username && <InlineError text={this.state.errors.username}/>}
</Form.Field>
<Form.Field error={!!this.state.errors.password}>
<label htmlFor="password" style = {{color: "white"}}>Password</label>
<input
className="Login-Inputs"
type="password"
id="password"
name="password"
placeholder="password"
value={this.state.data.password}
onChange={this.onChange}
/>
{this.state.errors.password && <InlineError text={this.state.errors.password}/>}
</Form.Field><br/>
<Button primary className = "Login-Button" style={{marginLeft: "7vw"}}>Login</Button>
</Form>
);
}
}
export default LoginForm;
The onSubmit is passed to it by its parent component which is as follows: onSubmit由其父组件传递给它,如下所示:
import React,{Component}from 'react';
import PropTypes from 'prop-types';
import LoginForm from '../forms/LoginForm';
import {connect} from 'react-redux';
import {Link} from 'react-router-dom';
import {login} from '../../actions/auth';
import './LoginPage.css';
import Header from '../Shared/Header(fixed)/headerFixed';
import TopMenu from '../Shared/topMenu';
class LoginPage extends Component{
constructor(props){
super(props);
this.submit=this.submit.bind(this);
}
submit=(data)=>{
return this.props.login(data)
.then(() => this.props.history.push("/forum"));
}
render(){
return(
<div className = "Login-Page-Body">
<Header/>
<TopMenu/>
<div className = "Login-Page-Head">
<h1 className = "Login-title">Login Page</h1>
<LoginForm submit={this.submit}/><br/>
<div className = "To-signup">Don't have an account? <Link to="/signup">Sign up here</Link></div>
</div>
</div>
)
}
}
LoginPage.propTypes= {
history: PropTypes.shape({
push: PropTypes.func.isRequired
}).isRequired,
login: PropTypes.func.isRequired
}
export default connect(null, {login})(LoginPage);
We connect this with the backend by the following code:我们通过以下代码将其与后端连接:
The file 'api.jsx' is as follows:文件“api.jsx”如下:
The Backend function for this route is as follows:该路由的后端function 如下:
@app.route('/backend/login', methods=["POST", "GET"])
def login():
content = request.get_json()
usern = content["credentials"]["username"]
password = content["credentials"]["password"]
# usern = "pranay_kothari"
# password = "1234"
# print(content["credentials"])
user = Login.query.filter(Login.username == usern, Login.password == password).first()
if user is None:
return 'ID does not exist', 501
token = encode_auth_token(usern)
dict = {
'token': token.decode(),
'username': usern,
'password': password
}
return make_response(jsonify(dict))
I know all this information might not be enough to figure out the problem so here is the link to the Github repository which contains my entire source code: Source Code我知道所有这些信息可能不足以找出问题,所以这里是指向 Github 存储库的链接,其中包含我的整个源代码:源代码
I am working on a full stack project for the first time so it would be great if you could also explain what is it that I am doing incorrectly.我是第一次从事全栈项目,所以如果你也能解释一下我做错了什么,那就太好了。
Please help!请帮忙!
.catch(err => this.setState({
errors: err.response.data.errors,
errors: {global: false},
loading:false
}));
err.response is undefined. err.response 未定义。 That means 'err' object does not have any property call response.
这意味着 'err' object 没有任何属性调用响应。 First look at the 'err' object first by,
首先看一下'err' object,
console.log(err);
To see what it contains.看看它包含什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.