Эта ошибка появляется в браузере при попытке отправить сообщение: новейшая ошибка:
TypeError: Невозможно прочитать свойство 'pu sh' undefined в SandwhichForm. js: 30 в asyn c SandwhichForm .handleFormSubmit (SandwhichForm. js: 27)
Вот полный код:
import React from 'react';
import { Form, Input, Button } from 'antd';
import { connect } from "react-redux";
import axios from 'axios';
const FormItem = Form.Item;
class SandwhichForm extends React.Component {
handleFormSubmit = async (event, requestType, sandwhichID) => {
event.preventDefault();
const postObj = {
title: event.target.elements.title.value,
content: event.target.elements.content.value
}
axios.defaults.xsrfHeaderName = "X-CSRFTOKEN";
axios.defaults.xsrfCookieName = "csrftoken";
axios.defaults.headers = {
"Content-Type": "application/json",
Authorization: `Token ${this.props.token}`,
};
if (requestType === "post") {
await axios.post('http://127.0.0.1:8000/api_sandwhiches/create/', postObj)
.then(res => {
if (res.status === 201) {
this.props.history.push(`/`);
}
})
.catch(error => console.error(error));
} else if (requestType === "put") {
await axios.put(`http://127.0.0.1:8000/api_sandwhiches/${sandwhichID}/update/`, postObj)
.then(res => {
if (res.status === 200) {
this.props.history.push(`/`);
}
})
.catch(error => console.error(error));
}
};
render() {
return (
<div>
<Form
onSubmit={event =>
this.handleFormSubmit(
event,
this.props.requestType,
this.props.sandwhichID
)
}
>
<FormItem label="Title">
<Input name = "title" placeholder="Put a title here" />
</FormItem>
<FormItem label="Content">
<Input name="content" placeholder="Enter some content ..." />
</FormItem>
<FormItem >
<Button type="primary" htmlType="submit">
{this.props.btnText}
</Button>
</FormItem>
</Form>
</div>
);
}
}
const mapStateToProps = state => {
return {
token: state.token
};
};
export default connect(mapStateToProps)(SandwhichForm)
Что происходит:
- Информация отправлялась и отправлялась в api
- ошибка теперь в консоли, а не в браузере
- информация больше не публикуется