I am trying to Post a form. When Post is successful the form gets cleared. However in case Post fails I don't want to clear the form. How should I update below code so form doesn't clear in case Post fails?
*const form = document.querySelector("#create-form")
const foodName = document.querySelector("#create-name")
const carbs = document.querySelector("#create-carbs")
const protein = document.querySelector("#create-protein")
const fat = document.querySelector("#create-fat")
form.addEventListener("submit", event => {
event.preventDefault()
//console.log(carbs.value)
const operation = async (name,carbs, protein, fat) =>{
const response = await fetch("https://firestore.googleapis.com/v1/projects/jsdemo-3f387/databases/(default)/documents/random12345610",{
method: "POST",
body: JSON.stringify({
fields: {
name: { stringValue: name },
carbs: { integerValue: carbs },
protein: { integerValue: protein },
fat: { integerValue: fat }
}
})
})
try{
const data = await response.json()}
catch(error){return;}
}
operation(foodName.value, carbs.value, protein.value, fat.value)
foodName.value = "";
carbs.value = "";
protein.value = "";
fat.value = "";
})*