Reputation: 186
I have some problem with PUT request in my reaсt app. I'm using redux-form with standart fetch request. When I send my request I got error: PUT http://localhost:8080/auth 500 (Internal Server Error) "SyntaxError: Unexpected token <" There is my code for fetch:
export default async function submit(fields, dispatch) {
console.trace()
const {email, fullName, company, industry, phone} = fields
let errors
if (!email)
errors = {
...errors,
email: 'Email cannot be empty'
}; else if (!/[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}/i.test(email))
errors = {...errors, email: 'Email seems invalid'};
if (!fullName)
errors = {...errors, fullName: 'Full name cannot be empty'};
if (!company)
errors = {...errors, company: 'Company name cannot be empty'};
if (!industry)
errors = {...errors, industry: 'Industry should be selected'};
if (errors) {
return Promise.reject(errors)
}
const response = await fetch('/auth', {
credentials: 'same-origin',
method: 'PUT',
body: Object.keys(fields)
.filter(prop => fields[prop] !== undefined)
.reduce((fd, prop) => {
fd.append(prop, fields[prop])
return fd
}, new FormData())
});
if (response.ok) {
console.log('good');
console.log(response);
} else {
const data = await response.json();
console.log('bad');
console.log(response);
}
}
What can be wrong with it?
Upvotes: 0
Views: 700
Reputation: 7272
This isn't really related to redux-form
, unless when you put a console.log(fields)
before your fetch()
call, you see something strange.
This is a serializing JSON into a FormData
question. That said, I can't really see anything wrong with your "reduce into FormData
" code. Maybe save that to a variable and output it to the console?
Either that, or there could be some problem on the server side where it is not expecting the data in that structure. I usually send stuff in plain JSON, but then I have full stack control.
Upvotes: 1