I am doing the assignment no 4 from the course "Front end web Development with React" in Coursera. Here I want to submit a form data to the server and then show an alert to the user. And The code works fine, the alert is also showing but then the error happens.
From related posts in stack overflow i figured out that the problem is somehow related to returning a redux action which must have a type property. But I cant figure out how i return this in this manner. As i don't have to do any action. I just need to show an alert.
export const postFeedback = (firstname, lastname, email, telnum, agree, contactType, message) => {
const newFeedback = {
firstname:firstname,
lastname:lastname,
email:email,
telnum:telnum,
agree:agree,
contactType: contactType,
message:message,
}
newFeedback.date = new Date().toISOString();
return fetch(baseUrl + 'feedback', {
method: "POST",
body: JSON.stringify(newFeedback),
headers: {
"content-Type": "application/json"
},
credentials: "same-origin"
})
.then(res => {
if (res.ok){
return res
}else{
var error = new Error('Error '+ res.status + ': '+res.statusText);
error.response = res;
throw error;
}},
error => {
throw error;
})
.then(response => response.json())
.then(response => alert(response))
.catch(error => { console.log('post feedback', error.message);
alert('Your feedback could not be sent\nError: '+error.message);
});
}
Everything works fine. the feedback is saved in the database and the alert is also shown and then the error "Error: Actions must be plain objects. Use custom middleware for async actions." happened.