'Axios post data shows in config, but not data, no body passed
I have a react frontend sending to a mongoose/express backend. Previously I was using flask for the backend and everything ran fine, this issue only started after switching to express. On postman, the call goes through perfect, but when I try through the browser, I get a 200 response, with an empty document created. In the console, the correct data shows in the config object, but not in the actual data object.
Heres the React/Formik onSubmit sending the post:
onSubmit={async (values) => {
console.log(values);
const headers = {
'Content-Type': 'application/json,multipart/form-data'
};
await axios.post('http://localhost:5000/api/clients', values, {headers})
.then(response => {
console.log(response);
return response;
})
//.then(this.props.history.push('/Clients'))
.catch(function (error){
console.log(error);
});
}}>
Heres the console response (the first object is the console.log(response) from the onSubmit):
Object { firstName: "Johnny", lastName: "Cochrane", phone: "8585678899", id: "" }
firstName: "Johnny"
id: ""
lastName: "Cochrane"
phone: "8585678899"
<prototype>: Object { … }
AddClient.js:49
Object { data: {…}, status: 200, statusText: "OK", headers: {…}, config: {…}, request: XMLHttpRequest }
config: Object { url: "http://localhost:5000/api/clients", method: "post", data: "{\"firstName\":\"Johnny\",\"lastName\":\"Cochrane\",\"phone\":\"8585678899\",\"id\":\"\"}", … }
data: Object { _id: "62043b723fe12d6b4ec354b8", clientId: 12, __v: 0, … }
headers: Object { "content-length": "71", "content-type": "application/json; charset=utf-8" }
request: XMLHttpRequest { readyState: 4, timeout: 0, withCredentials: false, … }
status: 200
statusText: "OK"
Any help would be greatly appreciated as I'm at a total loss. Thanks
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
