I'm using the fetch api to post form data, and I wanted fetch to console.log the data when the form data is finally saved to the database. So when I add the code res.json(farmer) after the data row was created the view that had the form gets replaced with the output of the saved data. Is there a way to send the data back to the fetch then function?Inside the home page``` document.onload = () => { const form = document.getElementById("form"); const data = new FormData(form); form.onsubmit = (e) =>{ e.preventDefault(); console.log(data.get("address")) fetch("/",{ method:"post", headers:{ "Content-Type" :"application/json" }, body:JSON.stringify(Object.fromEntries(data.entries())) }) .then(res => res.text()) .then(res => { console.log(res) e.target.reset() }) .catch(e => console.log(e)) }//onsubmit } ```Inside the controller``` module.exports.create = (req , res) => { Farmer.create(data).then(farmer => { res.json(farmer); }) .catch(err => console.log(err)) } ```
Submitted September 06, 2020 at 05:28AM by Design_Newbie
No comments:
Post a Comment