Why is my axios post request not sending data? I got a response which means the fields are empty
I don't know why the data is not sent to the backend.
When I test the API on Insomnia it works fine. I even use the API to add new users to the database, but when I try to add new users from react using redux and axios, it doesn't work, instead I get a response saying that the fields cannot be null.
I'm trying to console.log all the data I send and populate all fields correctly. I do not know what I did wrong.
I'm using express-validator to check input as well as normal error handling, that's why the response I get is empty.
This is my code to send data in redux
export const signin = data => dispatch => {
const config = {
method : "post",
url : "http://localhost:5000/user/signin",
headers : {
"Content-Type":"application/json"
},
body : JSON.stringify(data)
}
console.log(config.body)
axios(config)
.then(res => dispatch({
type : LOGIN_SUCCESS,
payload : res.data
}))
.catch(err => {
dispatch(error_msg(err.response.data, err.response.status))
dispatch({
type : LOGIN_FAIL
})
})
}
react form component
import React, { useState } from 'react'
import { Link } from 'react-router-dom'
import AuthLayout from './AuthLayout'
import Layout from './Layout'
import {connect} from 'react-redux'
import {signin} from '../store/actions/authAction'
import axios from 'axios'
function SignIn({signin}) {
const [value, setValue] = useState({
email : '',
password : ''
})
const handleChange = (e) => {
setValue({
...value,
[e.target.name] : e.target.value
})
}
const handleSubmit = e => {
e.preventDefault()
const {email, password} = value
const oldUser = {
email,
password
}
axios({method : "post",
url : "http://localhost:5000/user/signin",
headers : {
"Content-Type":"application/json; charset=UTF-8",
"Accept":"Token",
"Access-Control-Allow-Orgin":"*",
},
body : JSON.stringify(oldUser)})
.then(res => console.log(res.data))
.catch(err => console.log(err.response.data))
}
return (
<AuthLayout>
<div className="form_container" style={bg}>
<div className="form_title">
<h2>Sign In</h2>
</div>
<form onSubmit={handleSubmit}>
<div className="form_div">
<input type="text" placeholder="Enter Email" name="email" value={value.email} onChange={handleChange} />
</div>
<div className="form_div">
<input type="number" placeholder="Enter Password" name="password" value={value.password} onChange={handleChange} />
</div>
<div className="form_div form_btn">
<button>Submit</button>
</div>
<div className="form_div checkbox">
<input type="checkbox" />
<h4>Remember me</h4>
</div>
</form>
<p>Don't have an account? <Link to="/signup"><span>Sign up</span></Link> </p>
</div>
</AuthLayout>
)
}
const bg = {
backgroundImage: 'url(image/Group1.svg)'
}
const mapStateToProps = state => {
return{
user : state.auth.user
}
}
export default connect(mapStateToProps, {signin})(SignIn)
I didn't include the backend because it worked just fine.
Thanks to everyone who helped me understand the problem, I appreciate all your efforts.
I tried some of the solutions given to me here, but it didn't work.
So what I did later was remove the body variable and replace it with the data from the config object, and it worked.
Here is the code:
export const signin = data => dispatch => {
const config = {
method : "post",
url : "http://localhost:5000/user/signin",
headers : {
"Content-Type":"application/json",
},
data : JSON.stringify(data)
}
console.log(config.data)
axios(config)
.then(res => dispatch({
type : LOGIN_SUCCESS,
payload : res.data
}))
.catch(err => {
dispatch(error_msg(err.response.data, err.response.status))
dispatch({
type : LOGIN_FAIL
})
})
}
I don't think if is the best solution, but in this case it worked for me.