I have an object and I want to check if its fields are null or not and then I want to send a request. here is my code:
const [newProduct, setNewProduct] = useState({
name: null,
cost: null,
size: null,
color: null,
material: null,
discount: null,
description: null,
category: null
});
const addNewProduct = () => {
if(newProduct.name && newProduct.cost && newProduct.size && newProduct.color && newProduct.material && newProduct.discount && newProduct.description && newProduct.category) {
let productFormData = new FormData();
productFormData.append('productInfo', JSON.stringify(newProduct));
productFormData.append('productImages', images.imageFiles);
const addUrl = "http://localhost:8080/cpnl/addproduct";
axios({
method: "POST",
url: addUrl,
data: productFormData,
headers: { "Content-Type": "multipart/form-data" }
})
.then((response) => {
console.log(response.data.msg);
})
.catch((response) => {
console.error(response);
});
}
};
Is there a way to make if statement shorter? do I need to define all fields to modify them?
Aucun commentaire:
Enregistrer un commentaire