Properties of state not setting [“This field is required.”] Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Detecting an undefined object propertyHow to efficiently count the number of keys/properties of an object in JavaScript?How do I check if an object has a property in JavaScript?How can I merge properties of two JavaScript objects dynamically?How do I remove a property from a JavaScript object?Setting “checked” for a checkbox with jQuery?Set a default parameter value for a JavaScript functionSort array of objects by string property valueIterate through object propertiesHow get value datapicker in react toobox custom?

Where and when has Thucydides been studied?

Why did Bronn offer to be Tyrion Lannister's champion in trial by combat?

Russian equivalents of おしゃれは足元から (Every good outfit starts with the shoes)

malloc in main() or malloc in another function: allocating memory for a struct and its members

Does the main washing effect of soap come from foam?

Does the transliteration of 'Dravidian' exist in Hindu scripture? Does 'Dravida' refer to a Geographical area or an ethnic group?

Flight departed from the gate 5 min before scheduled departure time. Refund options

What is the proper term for etching or digging of wall to hide conduit of cables

How do you write "wild blueberries flavored"?

Random body shuffle every night—can we still function?

The Nth Gryphon Number

What is a more techy Technical Writer job title that isn't cutesy or confusing?

Did any compiler fully use 80-bit floating point?

What is "Lambda" in Heston's original paper on stochastic volatility models?

Why can't fire hurt Daenerys but it did to Jon Snow in season 1?

Does a random sequence of vectors span a Hilbert space?

How can I list files in reverse time order by a command and pass them as arguments to another command?

Is it OK to use the testing sample to compare algorithms?

How many time has Arya actually used Needle?

How do I say "this must not happen"?

Why is there so little support for joining EFTA in the British parliament?

Can two people see the same photon?

Is this Half-dragon Quaggoth boss monster balanced?

Why complex landing gears are used instead of simple, reliable and light weight muscle wire or shape memory alloys?



Properties of state not setting [“This field is required.”]



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!Detecting an undefined object propertyHow to efficiently count the number of keys/properties of an object in JavaScript?How do I check if an object has a property in JavaScript?How can I merge properties of two JavaScript objects dynamically?How do I remove a property from a JavaScript object?Setting “checked” for a checkbox with jQuery?Set a default parameter value for a JavaScript functionSort array of objects by string property valueIterate through object propertiesHow get value datapicker in react toobox custom?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I have a django/react (s3 bucket for image upload)project which adds recipes to a postgres database. In my RecipeFormContainer Component one of my properties to the recipe is an Array of ingredients. The array is supposed to be filled with objects that are made up of the key value pairs of units, quantity etc. I have written a method called addIngredients, but it does not seem to be adding the objects into my ingredients array. I get the error "ingredients: ["This field is required."]" when trying to submit my recipe and do a post. Please enlighten me with what I am missing.
Code is as follows:






import React, Component from 'react';
import Form from 'react-bootstrap'
import Button from 'react-bootstrap/Button';


class RecipeFormContainer extends Component
constructor(props)
super(props);

this.state =
title: '',
creator: '',
mealTime: "",
prepTime: "",
cookTime: "",
image_preview: "",
servings: "",
directions: '',
ingredients: [
units: '',
amounts: '',
multiples: '',
quantity: '',
name: ''
],
image: "",

;

this.handleImage = this.handleImage.bind(this);
this.handleIngredientInput = this.handleIngredientInput.bind(this);
this.handleAddIngredients = this.handleAddIngredients.bind(this);


handleAddIngredients =(e) =>
e.preventDefault();

// the ingredients properties are not being added to the ingredients array property on state *************************

let ingredient = units: '',
amounts: '',
multiples: '',
quantity: '',
name: '';

//save the current state of ingredients array in variable ingredient
let ingredients = this.state.ingredients;
// add the ingredient object into the ingredients array ( which is a property of state)
ingredients.push(ingredient);

// set the new array of ingredients as the state of the property ingredients
this.setState( ingredients: ingredients);
;



handleInput = (e) =>
e.preventDefault();
this.setState([e.target.name]: e.target.value);

;


handleIngredientInput = (e) =>
e.preventDefault();
this.setState([e.target.name]: e.target.value)
;

handleImage(event)
event.preventDefault();
// this makes it show up in preview

let file = event.target.files[0];
let fileReader = new FileReader();
fileReader.onloadend = () => this.setState(image_preview:fileReader.result);
fileReader.readAsDataURL(file);
this.setState(image:file);




submitRecipe = (event) =>
event.preventDefault();
let recipe = ...this.state;
console.log('recipe one', this.state);

let formData = new FormData();

formData.append("image", this.state.image);
formData.append('title', this.state.title);
formData.append("ingredients", this.state.ingredients);
formData.append("mealTime", this.state.mealTime);
formData.append("prepTime", this.state.prepTime);
formData.append("cookTime", this.state.cookTime);
formData.append("image_Preview", this.state.image_preview);
formData.append("servings", this.state.servings);
formData.append("directions", this.state.directions);
formData.append("creator", this.state.creator);
// formData.append("units", this.state.ingredients.units);
// formData.append("amount", this.state.ingredients.amount);
// formData.append("multiples", this.state.ingredients.multiples);
// formData.append("quantity", this.state.ingredients.quantity);
// formData.append("name", this.state.ingredients.name);

// add line for each property of state


const conf =
method: "POST",
body: formData,

;

fetch('/api/recipe/', conf).then((response) =>

return response.json();

).then((json) =>
this.props.addRecipe(json);

);


;


render()


return (


<Form onSubmit=this.submitRecipe encType="multipart/form-data" >

<Form.Group onSubmit=event => event.preventDefault(); >
<img src=this.state.image_preview/>
<input className="input" type="file" onChange=this.handleImage name="image" />
</Form.Group>

<Form.Group controlId="exampleForm.ControlInput1">
<Form.Label>Recipe Name</Form.Label>
<Form.Control type="text" placeholder="Enter Recipe Name Here"
name="title"
value=this.state.title
onChange=this.handleInput/>


<Form.Label>Recipe Creator</Form.Label>
<Form.Control type="text" placeholder="Enter Your Name Here"
value=this.state.creator
name="creator"
onChange=this.handleInput/>

</Form.Group>

<Form.Group controlId="exampleForm.ControlSelect1" id="foodType">
<Form.Label>Example select</Form.Label>
<Form.Control as="select">
<option>Breakfast</option>
<option>Lunch</option>
<option>Dinner</option>
<option>Dessert</option>
<option>Vegetarian</option>
</Form.Control>
</Form.Group>


<Form.Control type="text" placeholder="Prep Time" className="midButt"
value=this.state.prepTime
name="prepTime"
onChange=this.handleInput/>
<Form.Control type="text" placeholder="Cook Time" className="midButt"
value=this.state.cookTime
name="cookTime"
onChange=this.handleInput/>


<Form.Group controlId="exampleForm.ControlSelect1" id="foodTemp">
<Form.Control as="select">
<option>Fahrenheit</option>
<option>Celsius</option>
</Form.Control>
</Form.Group>


<Form.Group>
This Recipe Will Make: <Form.Control type="number" placeholder="servings" id="servings" value=this.state.ingredients.servings onChange=this.handleInput name="servings" />

<Form.Control type="text" placeholder="cookies, loaves, etc." id="loaf" value=this.state.ingredients.multiples onChange=this.handleIngredientInput name="multiples"/>

</Form.Group>

this.state.ingredients.map((ingredient, index) =>
return (
<div key=index>
<Form.Control type="number" placeholder="#" id="numberAmount" value=this.state.ingredients.quantity onChange=this.handleIngredientInput name="amount"/>
<Form.Control type="text" placeholder="units" id="units" value=this.state.ingredients.units onChange=this.handleIngredientInput name="units"/>
<Form.Control type="text" placeholder="Ingredient Name" id="name" value=this.state.ingredients.name onChange=this.handleIngredientInput name="name"/>
</div>
);
);

<Button variant="light" onClick = this.handleAddIngredients> + </Button>
/*/!*will update state with event handler this.state.ingredients, append object to array *!/*/


<Form.Group controlId="exampleForm.ControlTextarea1">
<Form.Label>Directions</Form.Label>
<Form.Control as="textarea" rows="3"
value=this.state.directions
name="directions"
onChange=this.handleInput/>
</Form.Group>


<Button type="submit" variant="secondary">Save This Recipe !</Button>

</Form>
)
;



export default RecipeFormContainer;

 












share|improve this question






















  • try to console.log your this.state.ingredients and look what's there when you add a new ingredient. Maybe the result isn't what you was expecting

    – mthrsj
    Mar 9 at 1:39












  • It is empty. My handleIngredientInput method is not doing what I expected. I am unsure of the syntax to make the logic I want work correctly. DO you have any advice ?

    – Christina Roberts
    Mar 9 at 11:35

















0















I have a django/react (s3 bucket for image upload)project which adds recipes to a postgres database. In my RecipeFormContainer Component one of my properties to the recipe is an Array of ingredients. The array is supposed to be filled with objects that are made up of the key value pairs of units, quantity etc. I have written a method called addIngredients, but it does not seem to be adding the objects into my ingredients array. I get the error "ingredients: ["This field is required."]" when trying to submit my recipe and do a post. Please enlighten me with what I am missing.
Code is as follows:






import React, Component from 'react';
import Form from 'react-bootstrap'
import Button from 'react-bootstrap/Button';


class RecipeFormContainer extends Component
constructor(props)
super(props);

this.state =
title: '',
creator: '',
mealTime: "",
prepTime: "",
cookTime: "",
image_preview: "",
servings: "",
directions: '',
ingredients: [
units: '',
amounts: '',
multiples: '',
quantity: '',
name: ''
],
image: "",

;

this.handleImage = this.handleImage.bind(this);
this.handleIngredientInput = this.handleIngredientInput.bind(this);
this.handleAddIngredients = this.handleAddIngredients.bind(this);


handleAddIngredients =(e) =>
e.preventDefault();

// the ingredients properties are not being added to the ingredients array property on state *************************

let ingredient = units: '',
amounts: '',
multiples: '',
quantity: '',
name: '';

//save the current state of ingredients array in variable ingredient
let ingredients = this.state.ingredients;
// add the ingredient object into the ingredients array ( which is a property of state)
ingredients.push(ingredient);

// set the new array of ingredients as the state of the property ingredients
this.setState( ingredients: ingredients);
;



handleInput = (e) =>
e.preventDefault();
this.setState([e.target.name]: e.target.value);

;


handleIngredientInput = (e) =>
e.preventDefault();
this.setState([e.target.name]: e.target.value)
;

handleImage(event)
event.preventDefault();
// this makes it show up in preview

let file = event.target.files[0];
let fileReader = new FileReader();
fileReader.onloadend = () => this.setState(image_preview:fileReader.result);
fileReader.readAsDataURL(file);
this.setState(image:file);




submitRecipe = (event) =>
event.preventDefault();
let recipe = ...this.state;
console.log('recipe one', this.state);

let formData = new FormData();

formData.append("image", this.state.image);
formData.append('title', this.state.title);
formData.append("ingredients", this.state.ingredients);
formData.append("mealTime", this.state.mealTime);
formData.append("prepTime", this.state.prepTime);
formData.append("cookTime", this.state.cookTime);
formData.append("image_Preview", this.state.image_preview);
formData.append("servings", this.state.servings);
formData.append("directions", this.state.directions);
formData.append("creator", this.state.creator);
// formData.append("units", this.state.ingredients.units);
// formData.append("amount", this.state.ingredients.amount);
// formData.append("multiples", this.state.ingredients.multiples);
// formData.append("quantity", this.state.ingredients.quantity);
// formData.append("name", this.state.ingredients.name);

// add line for each property of state


const conf =
method: "POST",
body: formData,

;

fetch('/api/recipe/', conf).then((response) =>

return response.json();

).then((json) =>
this.props.addRecipe(json);

);


;


render()


return (


<Form onSubmit=this.submitRecipe encType="multipart/form-data" >

<Form.Group onSubmit=event => event.preventDefault(); >
<img src=this.state.image_preview/>
<input className="input" type="file" onChange=this.handleImage name="image" />
</Form.Group>

<Form.Group controlId="exampleForm.ControlInput1">
<Form.Label>Recipe Name</Form.Label>
<Form.Control type="text" placeholder="Enter Recipe Name Here"
name="title"
value=this.state.title
onChange=this.handleInput/>


<Form.Label>Recipe Creator</Form.Label>
<Form.Control type="text" placeholder="Enter Your Name Here"
value=this.state.creator
name="creator"
onChange=this.handleInput/>

</Form.Group>

<Form.Group controlId="exampleForm.ControlSelect1" id="foodType">
<Form.Label>Example select</Form.Label>
<Form.Control as="select">
<option>Breakfast</option>
<option>Lunch</option>
<option>Dinner</option>
<option>Dessert</option>
<option>Vegetarian</option>
</Form.Control>
</Form.Group>


<Form.Control type="text" placeholder="Prep Time" className="midButt"
value=this.state.prepTime
name="prepTime"
onChange=this.handleInput/>
<Form.Control type="text" placeholder="Cook Time" className="midButt"
value=this.state.cookTime
name="cookTime"
onChange=this.handleInput/>


<Form.Group controlId="exampleForm.ControlSelect1" id="foodTemp">
<Form.Control as="select">
<option>Fahrenheit</option>
<option>Celsius</option>
</Form.Control>
</Form.Group>


<Form.Group>
This Recipe Will Make: <Form.Control type="number" placeholder="servings" id="servings" value=this.state.ingredients.servings onChange=this.handleInput name="servings" />

<Form.Control type="text" placeholder="cookies, loaves, etc." id="loaf" value=this.state.ingredients.multiples onChange=this.handleIngredientInput name="multiples"/>

</Form.Group>

this.state.ingredients.map((ingredient, index) =>
return (
<div key=index>
<Form.Control type="number" placeholder="#" id="numberAmount" value=this.state.ingredients.quantity onChange=this.handleIngredientInput name="amount"/>
<Form.Control type="text" placeholder="units" id="units" value=this.state.ingredients.units onChange=this.handleIngredientInput name="units"/>
<Form.Control type="text" placeholder="Ingredient Name" id="name" value=this.state.ingredients.name onChange=this.handleIngredientInput name="name"/>
</div>
);
);

<Button variant="light" onClick = this.handleAddIngredients> + </Button>
/*/!*will update state with event handler this.state.ingredients, append object to array *!/*/


<Form.Group controlId="exampleForm.ControlTextarea1">
<Form.Label>Directions</Form.Label>
<Form.Control as="textarea" rows="3"
value=this.state.directions
name="directions"
onChange=this.handleInput/>
</Form.Group>


<Button type="submit" variant="secondary">Save This Recipe !</Button>

</Form>
)
;



export default RecipeFormContainer;

 












share|improve this question






















  • try to console.log your this.state.ingredients and look what's there when you add a new ingredient. Maybe the result isn't what you was expecting

    – mthrsj
    Mar 9 at 1:39












  • It is empty. My handleIngredientInput method is not doing what I expected. I am unsure of the syntax to make the logic I want work correctly. DO you have any advice ?

    – Christina Roberts
    Mar 9 at 11:35













0












0








0


1






I have a django/react (s3 bucket for image upload)project which adds recipes to a postgres database. In my RecipeFormContainer Component one of my properties to the recipe is an Array of ingredients. The array is supposed to be filled with objects that are made up of the key value pairs of units, quantity etc. I have written a method called addIngredients, but it does not seem to be adding the objects into my ingredients array. I get the error "ingredients: ["This field is required."]" when trying to submit my recipe and do a post. Please enlighten me with what I am missing.
Code is as follows:






import React, Component from 'react';
import Form from 'react-bootstrap'
import Button from 'react-bootstrap/Button';


class RecipeFormContainer extends Component
constructor(props)
super(props);

this.state =
title: '',
creator: '',
mealTime: "",
prepTime: "",
cookTime: "",
image_preview: "",
servings: "",
directions: '',
ingredients: [
units: '',
amounts: '',
multiples: '',
quantity: '',
name: ''
],
image: "",

;

this.handleImage = this.handleImage.bind(this);
this.handleIngredientInput = this.handleIngredientInput.bind(this);
this.handleAddIngredients = this.handleAddIngredients.bind(this);


handleAddIngredients =(e) =>
e.preventDefault();

// the ingredients properties are not being added to the ingredients array property on state *************************

let ingredient = units: '',
amounts: '',
multiples: '',
quantity: '',
name: '';

//save the current state of ingredients array in variable ingredient
let ingredients = this.state.ingredients;
// add the ingredient object into the ingredients array ( which is a property of state)
ingredients.push(ingredient);

// set the new array of ingredients as the state of the property ingredients
this.setState( ingredients: ingredients);
;



handleInput = (e) =>
e.preventDefault();
this.setState([e.target.name]: e.target.value);

;


handleIngredientInput = (e) =>
e.preventDefault();
this.setState([e.target.name]: e.target.value)
;

handleImage(event)
event.preventDefault();
// this makes it show up in preview

let file = event.target.files[0];
let fileReader = new FileReader();
fileReader.onloadend = () => this.setState(image_preview:fileReader.result);
fileReader.readAsDataURL(file);
this.setState(image:file);




submitRecipe = (event) =>
event.preventDefault();
let recipe = ...this.state;
console.log('recipe one', this.state);

let formData = new FormData();

formData.append("image", this.state.image);
formData.append('title', this.state.title);
formData.append("ingredients", this.state.ingredients);
formData.append("mealTime", this.state.mealTime);
formData.append("prepTime", this.state.prepTime);
formData.append("cookTime", this.state.cookTime);
formData.append("image_Preview", this.state.image_preview);
formData.append("servings", this.state.servings);
formData.append("directions", this.state.directions);
formData.append("creator", this.state.creator);
// formData.append("units", this.state.ingredients.units);
// formData.append("amount", this.state.ingredients.amount);
// formData.append("multiples", this.state.ingredients.multiples);
// formData.append("quantity", this.state.ingredients.quantity);
// formData.append("name", this.state.ingredients.name);

// add line for each property of state


const conf =
method: "POST",
body: formData,

;

fetch('/api/recipe/', conf).then((response) =>

return response.json();

).then((json) =>
this.props.addRecipe(json);

);


;


render()


return (


<Form onSubmit=this.submitRecipe encType="multipart/form-data" >

<Form.Group onSubmit=event => event.preventDefault(); >
<img src=this.state.image_preview/>
<input className="input" type="file" onChange=this.handleImage name="image" />
</Form.Group>

<Form.Group controlId="exampleForm.ControlInput1">
<Form.Label>Recipe Name</Form.Label>
<Form.Control type="text" placeholder="Enter Recipe Name Here"
name="title"
value=this.state.title
onChange=this.handleInput/>


<Form.Label>Recipe Creator</Form.Label>
<Form.Control type="text" placeholder="Enter Your Name Here"
value=this.state.creator
name="creator"
onChange=this.handleInput/>

</Form.Group>

<Form.Group controlId="exampleForm.ControlSelect1" id="foodType">
<Form.Label>Example select</Form.Label>
<Form.Control as="select">
<option>Breakfast</option>
<option>Lunch</option>
<option>Dinner</option>
<option>Dessert</option>
<option>Vegetarian</option>
</Form.Control>
</Form.Group>


<Form.Control type="text" placeholder="Prep Time" className="midButt"
value=this.state.prepTime
name="prepTime"
onChange=this.handleInput/>
<Form.Control type="text" placeholder="Cook Time" className="midButt"
value=this.state.cookTime
name="cookTime"
onChange=this.handleInput/>


<Form.Group controlId="exampleForm.ControlSelect1" id="foodTemp">
<Form.Control as="select">
<option>Fahrenheit</option>
<option>Celsius</option>
</Form.Control>
</Form.Group>


<Form.Group>
This Recipe Will Make: <Form.Control type="number" placeholder="servings" id="servings" value=this.state.ingredients.servings onChange=this.handleInput name="servings" />

<Form.Control type="text" placeholder="cookies, loaves, etc." id="loaf" value=this.state.ingredients.multiples onChange=this.handleIngredientInput name="multiples"/>

</Form.Group>

this.state.ingredients.map((ingredient, index) =>
return (
<div key=index>
<Form.Control type="number" placeholder="#" id="numberAmount" value=this.state.ingredients.quantity onChange=this.handleIngredientInput name="amount"/>
<Form.Control type="text" placeholder="units" id="units" value=this.state.ingredients.units onChange=this.handleIngredientInput name="units"/>
<Form.Control type="text" placeholder="Ingredient Name" id="name" value=this.state.ingredients.name onChange=this.handleIngredientInput name="name"/>
</div>
);
);

<Button variant="light" onClick = this.handleAddIngredients> + </Button>
/*/!*will update state with event handler this.state.ingredients, append object to array *!/*/


<Form.Group controlId="exampleForm.ControlTextarea1">
<Form.Label>Directions</Form.Label>
<Form.Control as="textarea" rows="3"
value=this.state.directions
name="directions"
onChange=this.handleInput/>
</Form.Group>


<Button type="submit" variant="secondary">Save This Recipe !</Button>

</Form>
)
;



export default RecipeFormContainer;

 












share|improve this question














I have a django/react (s3 bucket for image upload)project which adds recipes to a postgres database. In my RecipeFormContainer Component one of my properties to the recipe is an Array of ingredients. The array is supposed to be filled with objects that are made up of the key value pairs of units, quantity etc. I have written a method called addIngredients, but it does not seem to be adding the objects into my ingredients array. I get the error "ingredients: ["This field is required."]" when trying to submit my recipe and do a post. Please enlighten me with what I am missing.
Code is as follows:






import React, Component from 'react';
import Form from 'react-bootstrap'
import Button from 'react-bootstrap/Button';


class RecipeFormContainer extends Component
constructor(props)
super(props);

this.state =
title: '',
creator: '',
mealTime: "",
prepTime: "",
cookTime: "",
image_preview: "",
servings: "",
directions: '',
ingredients: [
units: '',
amounts: '',
multiples: '',
quantity: '',
name: ''
],
image: "",

;

this.handleImage = this.handleImage.bind(this);
this.handleIngredientInput = this.handleIngredientInput.bind(this);
this.handleAddIngredients = this.handleAddIngredients.bind(this);


handleAddIngredients =(e) =>
e.preventDefault();

// the ingredients properties are not being added to the ingredients array property on state *************************

let ingredient = units: '',
amounts: '',
multiples: '',
quantity: '',
name: '';

//save the current state of ingredients array in variable ingredient
let ingredients = this.state.ingredients;
// add the ingredient object into the ingredients array ( which is a property of state)
ingredients.push(ingredient);

// set the new array of ingredients as the state of the property ingredients
this.setState( ingredients: ingredients);
;



handleInput = (e) =>
e.preventDefault();
this.setState([e.target.name]: e.target.value);

;


handleIngredientInput = (e) =>
e.preventDefault();
this.setState([e.target.name]: e.target.value)
;

handleImage(event)
event.preventDefault();
// this makes it show up in preview

let file = event.target.files[0];
let fileReader = new FileReader();
fileReader.onloadend = () => this.setState(image_preview:fileReader.result);
fileReader.readAsDataURL(file);
this.setState(image:file);




submitRecipe = (event) =>
event.preventDefault();
let recipe = ...this.state;
console.log('recipe one', this.state);

let formData = new FormData();

formData.append("image", this.state.image);
formData.append('title', this.state.title);
formData.append("ingredients", this.state.ingredients);
formData.append("mealTime", this.state.mealTime);
formData.append("prepTime", this.state.prepTime);
formData.append("cookTime", this.state.cookTime);
formData.append("image_Preview", this.state.image_preview);
formData.append("servings", this.state.servings);
formData.append("directions", this.state.directions);
formData.append("creator", this.state.creator);
// formData.append("units", this.state.ingredients.units);
// formData.append("amount", this.state.ingredients.amount);
// formData.append("multiples", this.state.ingredients.multiples);
// formData.append("quantity", this.state.ingredients.quantity);
// formData.append("name", this.state.ingredients.name);

// add line for each property of state


const conf =
method: "POST",
body: formData,

;

fetch('/api/recipe/', conf).then((response) =>

return response.json();

).then((json) =>
this.props.addRecipe(json);

);


;


render()


return (


<Form onSubmit=this.submitRecipe encType="multipart/form-data" >

<Form.Group onSubmit=event => event.preventDefault(); >
<img src=this.state.image_preview/>
<input className="input" type="file" onChange=this.handleImage name="image" />
</Form.Group>

<Form.Group controlId="exampleForm.ControlInput1">
<Form.Label>Recipe Name</Form.Label>
<Form.Control type="text" placeholder="Enter Recipe Name Here"
name="title"
value=this.state.title
onChange=this.handleInput/>


<Form.Label>Recipe Creator</Form.Label>
<Form.Control type="text" placeholder="Enter Your Name Here"
value=this.state.creator
name="creator"
onChange=this.handleInput/>

</Form.Group>

<Form.Group controlId="exampleForm.ControlSelect1" id="foodType">
<Form.Label>Example select</Form.Label>
<Form.Control as="select">
<option>Breakfast</option>
<option>Lunch</option>
<option>Dinner</option>
<option>Dessert</option>
<option>Vegetarian</option>
</Form.Control>
</Form.Group>


<Form.Control type="text" placeholder="Prep Time" className="midButt"
value=this.state.prepTime
name="prepTime"
onChange=this.handleInput/>
<Form.Control type="text" placeholder="Cook Time" className="midButt"
value=this.state.cookTime
name="cookTime"
onChange=this.handleInput/>


<Form.Group controlId="exampleForm.ControlSelect1" id="foodTemp">
<Form.Control as="select">
<option>Fahrenheit</option>
<option>Celsius</option>
</Form.Control>
</Form.Group>


<Form.Group>
This Recipe Will Make: <Form.Control type="number" placeholder="servings" id="servings" value=this.state.ingredients.servings onChange=this.handleInput name="servings" />

<Form.Control type="text" placeholder="cookies, loaves, etc." id="loaf" value=this.state.ingredients.multiples onChange=this.handleIngredientInput name="multiples"/>

</Form.Group>

this.state.ingredients.map((ingredient, index) =>
return (
<div key=index>
<Form.Control type="number" placeholder="#" id="numberAmount" value=this.state.ingredients.quantity onChange=this.handleIngredientInput name="amount"/>
<Form.Control type="text" placeholder="units" id="units" value=this.state.ingredients.units onChange=this.handleIngredientInput name="units"/>
<Form.Control type="text" placeholder="Ingredient Name" id="name" value=this.state.ingredients.name onChange=this.handleIngredientInput name="name"/>
</div>
);
);

<Button variant="light" onClick = this.handleAddIngredients> + </Button>
/*/!*will update state with event handler this.state.ingredients, append object to array *!/*/


<Form.Group controlId="exampleForm.ControlTextarea1">
<Form.Label>Directions</Form.Label>
<Form.Control as="textarea" rows="3"
value=this.state.directions
name="directions"
onChange=this.handleInput/>
</Form.Group>


<Button type="submit" variant="secondary">Save This Recipe !</Button>

</Form>
)
;



export default RecipeFormContainer;

 








import React, Component from 'react';
import Form from 'react-bootstrap'
import Button from 'react-bootstrap/Button';


class RecipeFormContainer extends Component
constructor(props)
super(props);

this.state =
title: '',
creator: '',
mealTime: "",
prepTime: "",
cookTime: "",
image_preview: "",
servings: "",
directions: '',
ingredients: [
units: '',
amounts: '',
multiples: '',
quantity: '',
name: ''
],
image: "",

;

this.handleImage = this.handleImage.bind(this);
this.handleIngredientInput = this.handleIngredientInput.bind(this);
this.handleAddIngredients = this.handleAddIngredients.bind(this);


handleAddIngredients =(e) =>
e.preventDefault();

// the ingredients properties are not being added to the ingredients array property on state *************************

let ingredient = units: '',
amounts: '',
multiples: '',
quantity: '',
name: '';

//save the current state of ingredients array in variable ingredient
let ingredients = this.state.ingredients;
// add the ingredient object into the ingredients array ( which is a property of state)
ingredients.push(ingredient);

// set the new array of ingredients as the state of the property ingredients
this.setState( ingredients: ingredients);
;



handleInput = (e) =>
e.preventDefault();
this.setState([e.target.name]: e.target.value);

;


handleIngredientInput = (e) =>
e.preventDefault();
this.setState([e.target.name]: e.target.value)
;

handleImage(event)
event.preventDefault();
// this makes it show up in preview

let file = event.target.files[0];
let fileReader = new FileReader();
fileReader.onloadend = () => this.setState(image_preview:fileReader.result);
fileReader.readAsDataURL(file);
this.setState(image:file);




submitRecipe = (event) =>
event.preventDefault();
let recipe = ...this.state;
console.log('recipe one', this.state);

let formData = new FormData();

formData.append("image", this.state.image);
formData.append('title', this.state.title);
formData.append("ingredients", this.state.ingredients);
formData.append("mealTime", this.state.mealTime);
formData.append("prepTime", this.state.prepTime);
formData.append("cookTime", this.state.cookTime);
formData.append("image_Preview", this.state.image_preview);
formData.append("servings", this.state.servings);
formData.append("directions", this.state.directions);
formData.append("creator", this.state.creator);
// formData.append("units", this.state.ingredients.units);
// formData.append("amount", this.state.ingredients.amount);
// formData.append("multiples", this.state.ingredients.multiples);
// formData.append("quantity", this.state.ingredients.quantity);
// formData.append("name", this.state.ingredients.name);

// add line for each property of state


const conf =
method: "POST",
body: formData,

;

fetch('/api/recipe/', conf).then((response) =>

return response.json();

).then((json) =>
this.props.addRecipe(json);

);


;


render()


return (


<Form onSubmit=this.submitRecipe encType="multipart/form-data" >

<Form.Group onSubmit=event => event.preventDefault(); >
<img src=this.state.image_preview/>
<input className="input" type="file" onChange=this.handleImage name="image" />
</Form.Group>

<Form.Group controlId="exampleForm.ControlInput1">
<Form.Label>Recipe Name</Form.Label>
<Form.Control type="text" placeholder="Enter Recipe Name Here"
name="title"
value=this.state.title
onChange=this.handleInput/>


<Form.Label>Recipe Creator</Form.Label>
<Form.Control type="text" placeholder="Enter Your Name Here"
value=this.state.creator
name="creator"
onChange=this.handleInput/>

</Form.Group>

<Form.Group controlId="exampleForm.ControlSelect1" id="foodType">
<Form.Label>Example select</Form.Label>
<Form.Control as="select">
<option>Breakfast</option>
<option>Lunch</option>
<option>Dinner</option>
<option>Dessert</option>
<option>Vegetarian</option>
</Form.Control>
</Form.Group>


<Form.Control type="text" placeholder="Prep Time" className="midButt"
value=this.state.prepTime
name="prepTime"
onChange=this.handleInput/>
<Form.Control type="text" placeholder="Cook Time" className="midButt"
value=this.state.cookTime
name="cookTime"
onChange=this.handleInput/>


<Form.Group controlId="exampleForm.ControlSelect1" id="foodTemp">
<Form.Control as="select">
<option>Fahrenheit</option>
<option>Celsius</option>
</Form.Control>
</Form.Group>


<Form.Group>
This Recipe Will Make: <Form.Control type="number" placeholder="servings" id="servings" value=this.state.ingredients.servings onChange=this.handleInput name="servings" />

<Form.Control type="text" placeholder="cookies, loaves, etc." id="loaf" value=this.state.ingredients.multiples onChange=this.handleIngredientInput name="multiples"/>

</Form.Group>

this.state.ingredients.map((ingredient, index) =>
return (
<div key=index>
<Form.Control type="number" placeholder="#" id="numberAmount" value=this.state.ingredients.quantity onChange=this.handleIngredientInput name="amount"/>
<Form.Control type="text" placeholder="units" id="units" value=this.state.ingredients.units onChange=this.handleIngredientInput name="units"/>
<Form.Control type="text" placeholder="Ingredient Name" id="name" value=this.state.ingredients.name onChange=this.handleIngredientInput name="name"/>
</div>
);
);

<Button variant="light" onClick = this.handleAddIngredients> + </Button>
/*/!*will update state with event handler this.state.ingredients, append object to array *!/*/


<Form.Group controlId="exampleForm.ControlTextarea1">
<Form.Label>Directions</Form.Label>
<Form.Control as="textarea" rows="3"
value=this.state.directions
name="directions"
onChange=this.handleInput/>
</Form.Group>


<Button type="submit" variant="secondary">Save This Recipe !</Button>

</Form>
)
;



export default RecipeFormContainer;

 





import React, Component from 'react';
import Form from 'react-bootstrap'
import Button from 'react-bootstrap/Button';


class RecipeFormContainer extends Component
constructor(props)
super(props);

this.state =
title: '',
creator: '',
mealTime: "",
prepTime: "",
cookTime: "",
image_preview: "",
servings: "",
directions: '',
ingredients: [
units: '',
amounts: '',
multiples: '',
quantity: '',
name: ''
],
image: "",

;

this.handleImage = this.handleImage.bind(this);
this.handleIngredientInput = this.handleIngredientInput.bind(this);
this.handleAddIngredients = this.handleAddIngredients.bind(this);


handleAddIngredients =(e) =>
e.preventDefault();

// the ingredients properties are not being added to the ingredients array property on state *************************

let ingredient = units: '',
amounts: '',
multiples: '',
quantity: '',
name: '';

//save the current state of ingredients array in variable ingredient
let ingredients = this.state.ingredients;
// add the ingredient object into the ingredients array ( which is a property of state)
ingredients.push(ingredient);

// set the new array of ingredients as the state of the property ingredients
this.setState( ingredients: ingredients);
;



handleInput = (e) =>
e.preventDefault();
this.setState([e.target.name]: e.target.value);

;


handleIngredientInput = (e) =>
e.preventDefault();
this.setState([e.target.name]: e.target.value)
;

handleImage(event)
event.preventDefault();
// this makes it show up in preview

let file = event.target.files[0];
let fileReader = new FileReader();
fileReader.onloadend = () => this.setState(image_preview:fileReader.result);
fileReader.readAsDataURL(file);
this.setState(image:file);




submitRecipe = (event) =>
event.preventDefault();
let recipe = ...this.state;
console.log('recipe one', this.state);

let formData = new FormData();

formData.append("image", this.state.image);
formData.append('title', this.state.title);
formData.append("ingredients", this.state.ingredients);
formData.append("mealTime", this.state.mealTime);
formData.append("prepTime", this.state.prepTime);
formData.append("cookTime", this.state.cookTime);
formData.append("image_Preview", this.state.image_preview);
formData.append("servings", this.state.servings);
formData.append("directions", this.state.directions);
formData.append("creator", this.state.creator);
// formData.append("units", this.state.ingredients.units);
// formData.append("amount", this.state.ingredients.amount);
// formData.append("multiples", this.state.ingredients.multiples);
// formData.append("quantity", this.state.ingredients.quantity);
// formData.append("name", this.state.ingredients.name);

// add line for each property of state


const conf =
method: "POST",
body: formData,

;

fetch('/api/recipe/', conf).then((response) =>

return response.json();

).then((json) =>
this.props.addRecipe(json);

);


;


render()


return (


<Form onSubmit=this.submitRecipe encType="multipart/form-data" >

<Form.Group onSubmit=event => event.preventDefault(); >
<img src=this.state.image_preview/>
<input className="input" type="file" onChange=this.handleImage name="image" />
</Form.Group>

<Form.Group controlId="exampleForm.ControlInput1">
<Form.Label>Recipe Name</Form.Label>
<Form.Control type="text" placeholder="Enter Recipe Name Here"
name="title"
value=this.state.title
onChange=this.handleInput/>


<Form.Label>Recipe Creator</Form.Label>
<Form.Control type="text" placeholder="Enter Your Name Here"
value=this.state.creator
name="creator"
onChange=this.handleInput/>

</Form.Group>

<Form.Group controlId="exampleForm.ControlSelect1" id="foodType">
<Form.Label>Example select</Form.Label>
<Form.Control as="select">
<option>Breakfast</option>
<option>Lunch</option>
<option>Dinner</option>
<option>Dessert</option>
<option>Vegetarian</option>
</Form.Control>
</Form.Group>


<Form.Control type="text" placeholder="Prep Time" className="midButt"
value=this.state.prepTime
name="prepTime"
onChange=this.handleInput/>
<Form.Control type="text" placeholder="Cook Time" className="midButt"
value=this.state.cookTime
name="cookTime"
onChange=this.handleInput/>


<Form.Group controlId="exampleForm.ControlSelect1" id="foodTemp">
<Form.Control as="select">
<option>Fahrenheit</option>
<option>Celsius</option>
</Form.Control>
</Form.Group>


<Form.Group>
This Recipe Will Make: <Form.Control type="number" placeholder="servings" id="servings" value=this.state.ingredients.servings onChange=this.handleInput name="servings" />

<Form.Control type="text" placeholder="cookies, loaves, etc." id="loaf" value=this.state.ingredients.multiples onChange=this.handleIngredientInput name="multiples"/>

</Form.Group>

this.state.ingredients.map((ingredient, index) =>
return (
<div key=index>
<Form.Control type="number" placeholder="#" id="numberAmount" value=this.state.ingredients.quantity onChange=this.handleIngredientInput name="amount"/>
<Form.Control type="text" placeholder="units" id="units" value=this.state.ingredients.units onChange=this.handleIngredientInput name="units"/>
<Form.Control type="text" placeholder="Ingredient Name" id="name" value=this.state.ingredients.name onChange=this.handleIngredientInput name="name"/>
</div>
);
);

<Button variant="light" onClick = this.handleAddIngredients> + </Button>
/*/!*will update state with event handler this.state.ingredients, append object to array *!/*/


<Form.Group controlId="exampleForm.ControlTextarea1">
<Form.Label>Directions</Form.Label>
<Form.Control as="textarea" rows="3"
value=this.state.directions
name="directions"
onChange=this.handleInput/>
</Form.Group>


<Button type="submit" variant="secondary">Save This Recipe !</Button>

</Form>
)
;



export default RecipeFormContainer;

 






javascript django reactjs amazon-s3 react-bootstrap






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 9 at 0:46









Christina RobertsChristina Roberts

1




1












  • try to console.log your this.state.ingredients and look what's there when you add a new ingredient. Maybe the result isn't what you was expecting

    – mthrsj
    Mar 9 at 1:39












  • It is empty. My handleIngredientInput method is not doing what I expected. I am unsure of the syntax to make the logic I want work correctly. DO you have any advice ?

    – Christina Roberts
    Mar 9 at 11:35

















  • try to console.log your this.state.ingredients and look what's there when you add a new ingredient. Maybe the result isn't what you was expecting

    – mthrsj
    Mar 9 at 1:39












  • It is empty. My handleIngredientInput method is not doing what I expected. I am unsure of the syntax to make the logic I want work correctly. DO you have any advice ?

    – Christina Roberts
    Mar 9 at 11:35
















try to console.log your this.state.ingredients and look what's there when you add a new ingredient. Maybe the result isn't what you was expecting

– mthrsj
Mar 9 at 1:39






try to console.log your this.state.ingredients and look what's there when you add a new ingredient. Maybe the result isn't what you was expecting

– mthrsj
Mar 9 at 1:39














It is empty. My handleIngredientInput method is not doing what I expected. I am unsure of the syntax to make the logic I want work correctly. DO you have any advice ?

– Christina Roberts
Mar 9 at 11:35





It is empty. My handleIngredientInput method is not doing what I expected. I am unsure of the syntax to make the logic I want work correctly. DO you have any advice ?

– Christina Roberts
Mar 9 at 11:35












1 Answer
1






active

oldest

votes


















0














Taking a look at your code, your handleAddIngredients isn't right. You commited two mistakes:



Not collecting data from the inputs



You're not collecting the data from the state to put inside the ingredient. Your code creates a blank ingredient to put on the array.



Trying to copy the ingredients array on the wrong way.



Your code "extracts" the ingredients from the this.state.ingredients, but it's not possible. Your ingredients belongs to your state, not to state.ingredients.



Instead of doing



let ingredients = this.state.ingredients


You could do



let ingredients = this.state


Or even



let ingredients = [ ...this.state.ingredients ]


Conclusion



After all the fixes, your code should look like below:



handleAddIngredients = (e) => 
e.preventDefault();

//Extracting the values from the state
let units, amounts, multiples, quantity, name = this.state

// Inserting the values into the new ingredient
let ingredient = units, amounts, multiples, quantity, name

// Creating the copy
let ingredients = [ ... this.state.ingredients ]

// add the ingredient object into the ingredients array ( which is a property of state)
ingredients.push(ingredient);

// set the new array of ingredients in the state
this.setState(ingredients);
;





share|improve this answer























  • Thank you so much for taking the time to explain that !I knew my methods were not working correctly, but was not sure how to fix them. I will try this right away !

    – Christina Roberts
    Mar 9 at 17:49











  • @ChristinaRoberts, you're welcome! I recommend to you to check if all the other handles are working as expected, also.

    – mthrsj
    Mar 9 at 17:54











  • that fixed my problem of being able to add the ingredients, but now further down, the post request is still not working. I am VERY new to the javascript and react side of things so its still a challenge to turn my logic (backend style) into valid javascript code haha

    – Christina Roberts
    Mar 9 at 18:16











  • @ChristinaRoberts, as this answer solves your problem of ingredients field being empty, feel free to mark it as solution. If is there any other problem, not related with the first one, I recommend you to ask a new question

    – mthrsj
    Mar 10 at 11:54











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55072890%2fproperties-of-state-not-setting-this-field-is-required%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Taking a look at your code, your handleAddIngredients isn't right. You commited two mistakes:



Not collecting data from the inputs



You're not collecting the data from the state to put inside the ingredient. Your code creates a blank ingredient to put on the array.



Trying to copy the ingredients array on the wrong way.



Your code "extracts" the ingredients from the this.state.ingredients, but it's not possible. Your ingredients belongs to your state, not to state.ingredients.



Instead of doing



let ingredients = this.state.ingredients


You could do



let ingredients = this.state


Or even



let ingredients = [ ...this.state.ingredients ]


Conclusion



After all the fixes, your code should look like below:



handleAddIngredients = (e) => 
e.preventDefault();

//Extracting the values from the state
let units, amounts, multiples, quantity, name = this.state

// Inserting the values into the new ingredient
let ingredient = units, amounts, multiples, quantity, name

// Creating the copy
let ingredients = [ ... this.state.ingredients ]

// add the ingredient object into the ingredients array ( which is a property of state)
ingredients.push(ingredient);

// set the new array of ingredients in the state
this.setState(ingredients);
;





share|improve this answer























  • Thank you so much for taking the time to explain that !I knew my methods were not working correctly, but was not sure how to fix them. I will try this right away !

    – Christina Roberts
    Mar 9 at 17:49











  • @ChristinaRoberts, you're welcome! I recommend to you to check if all the other handles are working as expected, also.

    – mthrsj
    Mar 9 at 17:54











  • that fixed my problem of being able to add the ingredients, but now further down, the post request is still not working. I am VERY new to the javascript and react side of things so its still a challenge to turn my logic (backend style) into valid javascript code haha

    – Christina Roberts
    Mar 9 at 18:16











  • @ChristinaRoberts, as this answer solves your problem of ingredients field being empty, feel free to mark it as solution. If is there any other problem, not related with the first one, I recommend you to ask a new question

    – mthrsj
    Mar 10 at 11:54















0














Taking a look at your code, your handleAddIngredients isn't right. You commited two mistakes:



Not collecting data from the inputs



You're not collecting the data from the state to put inside the ingredient. Your code creates a blank ingredient to put on the array.



Trying to copy the ingredients array on the wrong way.



Your code "extracts" the ingredients from the this.state.ingredients, but it's not possible. Your ingredients belongs to your state, not to state.ingredients.



Instead of doing



let ingredients = this.state.ingredients


You could do



let ingredients = this.state


Or even



let ingredients = [ ...this.state.ingredients ]


Conclusion



After all the fixes, your code should look like below:



handleAddIngredients = (e) => 
e.preventDefault();

//Extracting the values from the state
let units, amounts, multiples, quantity, name = this.state

// Inserting the values into the new ingredient
let ingredient = units, amounts, multiples, quantity, name

// Creating the copy
let ingredients = [ ... this.state.ingredients ]

// add the ingredient object into the ingredients array ( which is a property of state)
ingredients.push(ingredient);

// set the new array of ingredients in the state
this.setState(ingredients);
;





share|improve this answer























  • Thank you so much for taking the time to explain that !I knew my methods were not working correctly, but was not sure how to fix them. I will try this right away !

    – Christina Roberts
    Mar 9 at 17:49











  • @ChristinaRoberts, you're welcome! I recommend to you to check if all the other handles are working as expected, also.

    – mthrsj
    Mar 9 at 17:54











  • that fixed my problem of being able to add the ingredients, but now further down, the post request is still not working. I am VERY new to the javascript and react side of things so its still a challenge to turn my logic (backend style) into valid javascript code haha

    – Christina Roberts
    Mar 9 at 18:16











  • @ChristinaRoberts, as this answer solves your problem of ingredients field being empty, feel free to mark it as solution. If is there any other problem, not related with the first one, I recommend you to ask a new question

    – mthrsj
    Mar 10 at 11:54













0












0








0







Taking a look at your code, your handleAddIngredients isn't right. You commited two mistakes:



Not collecting data from the inputs



You're not collecting the data from the state to put inside the ingredient. Your code creates a blank ingredient to put on the array.



Trying to copy the ingredients array on the wrong way.



Your code "extracts" the ingredients from the this.state.ingredients, but it's not possible. Your ingredients belongs to your state, not to state.ingredients.



Instead of doing



let ingredients = this.state.ingredients


You could do



let ingredients = this.state


Or even



let ingredients = [ ...this.state.ingredients ]


Conclusion



After all the fixes, your code should look like below:



handleAddIngredients = (e) => 
e.preventDefault();

//Extracting the values from the state
let units, amounts, multiples, quantity, name = this.state

// Inserting the values into the new ingredient
let ingredient = units, amounts, multiples, quantity, name

// Creating the copy
let ingredients = [ ... this.state.ingredients ]

// add the ingredient object into the ingredients array ( which is a property of state)
ingredients.push(ingredient);

// set the new array of ingredients in the state
this.setState(ingredients);
;





share|improve this answer













Taking a look at your code, your handleAddIngredients isn't right. You commited two mistakes:



Not collecting data from the inputs



You're not collecting the data from the state to put inside the ingredient. Your code creates a blank ingredient to put on the array.



Trying to copy the ingredients array on the wrong way.



Your code "extracts" the ingredients from the this.state.ingredients, but it's not possible. Your ingredients belongs to your state, not to state.ingredients.



Instead of doing



let ingredients = this.state.ingredients


You could do



let ingredients = this.state


Or even



let ingredients = [ ...this.state.ingredients ]


Conclusion



After all the fixes, your code should look like below:



handleAddIngredients = (e) => 
e.preventDefault();

//Extracting the values from the state
let units, amounts, multiples, quantity, name = this.state

// Inserting the values into the new ingredient
let ingredient = units, amounts, multiples, quantity, name

// Creating the copy
let ingredients = [ ... this.state.ingredients ]

// add the ingredient object into the ingredients array ( which is a property of state)
ingredients.push(ingredient);

// set the new array of ingredients in the state
this.setState(ingredients);
;






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 9 at 17:23









mthrsjmthrsj

1,6541724




1,6541724












  • Thank you so much for taking the time to explain that !I knew my methods were not working correctly, but was not sure how to fix them. I will try this right away !

    – Christina Roberts
    Mar 9 at 17:49











  • @ChristinaRoberts, you're welcome! I recommend to you to check if all the other handles are working as expected, also.

    – mthrsj
    Mar 9 at 17:54











  • that fixed my problem of being able to add the ingredients, but now further down, the post request is still not working. I am VERY new to the javascript and react side of things so its still a challenge to turn my logic (backend style) into valid javascript code haha

    – Christina Roberts
    Mar 9 at 18:16











  • @ChristinaRoberts, as this answer solves your problem of ingredients field being empty, feel free to mark it as solution. If is there any other problem, not related with the first one, I recommend you to ask a new question

    – mthrsj
    Mar 10 at 11:54

















  • Thank you so much for taking the time to explain that !I knew my methods were not working correctly, but was not sure how to fix them. I will try this right away !

    – Christina Roberts
    Mar 9 at 17:49











  • @ChristinaRoberts, you're welcome! I recommend to you to check if all the other handles are working as expected, also.

    – mthrsj
    Mar 9 at 17:54











  • that fixed my problem of being able to add the ingredients, but now further down, the post request is still not working. I am VERY new to the javascript and react side of things so its still a challenge to turn my logic (backend style) into valid javascript code haha

    – Christina Roberts
    Mar 9 at 18:16











  • @ChristinaRoberts, as this answer solves your problem of ingredients field being empty, feel free to mark it as solution. If is there any other problem, not related with the first one, I recommend you to ask a new question

    – mthrsj
    Mar 10 at 11:54
















Thank you so much for taking the time to explain that !I knew my methods were not working correctly, but was not sure how to fix them. I will try this right away !

– Christina Roberts
Mar 9 at 17:49





Thank you so much for taking the time to explain that !I knew my methods were not working correctly, but was not sure how to fix them. I will try this right away !

– Christina Roberts
Mar 9 at 17:49













@ChristinaRoberts, you're welcome! I recommend to you to check if all the other handles are working as expected, also.

– mthrsj
Mar 9 at 17:54





@ChristinaRoberts, you're welcome! I recommend to you to check if all the other handles are working as expected, also.

– mthrsj
Mar 9 at 17:54













that fixed my problem of being able to add the ingredients, but now further down, the post request is still not working. I am VERY new to the javascript and react side of things so its still a challenge to turn my logic (backend style) into valid javascript code haha

– Christina Roberts
Mar 9 at 18:16





that fixed my problem of being able to add the ingredients, but now further down, the post request is still not working. I am VERY new to the javascript and react side of things so its still a challenge to turn my logic (backend style) into valid javascript code haha

– Christina Roberts
Mar 9 at 18:16













@ChristinaRoberts, as this answer solves your problem of ingredients field being empty, feel free to mark it as solution. If is there any other problem, not related with the first one, I recommend you to ask a new question

– mthrsj
Mar 10 at 11:54





@ChristinaRoberts, as this answer solves your problem of ingredients field being empty, feel free to mark it as solution. If is there any other problem, not related with the first one, I recommend you to ask a new question

– mthrsj
Mar 10 at 11:54



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55072890%2fproperties-of-state-not-setting-this-field-is-required%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved