I am trying to post data with axios with a formik form but when I checked my backend, data keeps returning undefined seems like the backend isn't receiving the data at all, and browser responds with AxiosError and server response 500, Moreover I am also getting another error A component is changing a controlled input to be uncontrolled after I submitted the form
this is my signup function with formik and axios.post
function Test() {
const validate = Yup.object({
name: Yup.string()
.max(15, 'Must be 15 characters or less')
.required('Required'),
email: Yup.string()
.email('Email is invalid')
.required('Email is required'),
password: Yup.string()
.min(6, 'Password must be at least 6 charaters')
.required('Password is required')
})
return (
<Formik
initialValues={{
name: '',
email: '',
password: ''
}}
validationSchema={validate}
onSubmit={data => {
console.log(data)
let formData = new FormData();
formData.append('name', data.name)
formData.append('email', data.email)
formData.append('password', data.password)
axios({
method: 'POST',
url: 'http://localhost:5000/userdata',
data: formData
})
.then(function (res) {
console.log(res)
alert('Successfully signed up!');
})
.catch(function (res) {
console.log(res)
});
}}
>
{formik => (
<div className='fbody'>
<div className='fcontainer'>
<div className="contact-box">
<div className="right">
<h1 className="my-4 font-weight-bold .display-4">Sign Up</h1>
<Form>
<TextField className="field" label="First Name" name="name" type="text" />
<TextField className="field" label="Email" name="email" type="email" />
<TextField className="field" label="password" name="password" type="password" />
<button className="btn" type="submit">Register</button>
<button className="btn" type="reset">Reset</button>
</Form>
</div>
</div>
</div>
</div>
)}
</Formik>
)
}
export default Test;