-
Hi@All 👋🏼 https://github.com/gwittm/Digital-Recipe-Book/tree/US3-FormularIngredients-Gabi |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Here is how I found the problem:
if (request.method === "POST") {
const recipes = request.body;
console.log("RECIPES", recipes);
...
export default function RecipeForm({ onSubmit, formName, defaultData }) {
function handleSubmit(event) {
event.preventDefault();
const formData = new FormData(event.target);
const data = Object.fromEntries(formData);
onSubmit(data);
}
const [ingredients, setIngredients] = useState([]);
...
export default function RecipeForm({ onSubmit, formName, defaultData }) {
const [ingredients, setIngredients] = useState([]);
function handleSubmit(event) {
event.preventDefault();
const formData = new FormData(event.target);
const data = Object.fromEntries(formData);
onSubmit({ ...data, ingredients });
}
...
{data.map((recipe, _id) => {
return (
<ListItem key={recipe._id}>
{recipe.title}
{recipe.ingredients.map((ingredient, index) => (
// ingredient is an object
<div key={index}>{ingredient}</div>
))}
</ListItem>
);
})} with addressed key: <div key={ingredient.id}>{ingredient.name}</div> Thats it! Hope that helps :) |
Beta Was this translation helpful? Give feedback.
Here is how I found the problem:
pages/api/recipes/index.js
and added a console.log to your POST method like this:After submitting a recipe and inspecting the log I recognized that the data I received had no ingredients included and was not how I expected it.
I went to
components/RecipeForm.js
and found that the ingredients are not passed to the onSubmit function