简体   繁体   English

我收到一个错误:无法读取未定义的属性“推送”

[英]I am getting an error: Cannot read property 'push' of undefined

I am currently working on a personal project creating a recipe-app.我目前正在开发一个创建配方应用程序的个人项目。 when I launch a dev-server, I am getting an error: ingredients.js:26 Uncaught TypeError: Cannot read property 'push' of undefined当我启动一个开发服务器时,我收到一个错误:成分.js:26 未捕获的类型错误:无法读取未定义的属性“推送”

I think, there is a problem with recipe.ingredients but when I tried to tweak it, I still couldn't solve it.我想,recipe.ingredients 有问题,但是当我尝试调整它时,我仍然无法解决它。 Could anyone please have a look at my source code, a file is called ingredients.js任何人都可以看看我的源代码,一个文件叫做成分.js

 import uuidv4 from 'uuid/v4' import { getRecipe, saveRecipe } from './recipes' const createIngredients = (recipeId, text) => { const recipes = getRecipe() const recipe = recipes.find((recipe) => recipe.id === recipeId) recipe.ingredients.push({ id: uuidv4(), text, completed: false }) saveRecipe() } const removeIngredients = (id, recipeId) => { const recipes = getRecipe() const recipe = recipes.find((recipe) => recipe.id === recipeId) const ingredients = recipe.ingredients const ingredientIndex = ingredients.find((ingredient) => ingredient.id === id) if (ingredientIndex > -1) { ingredients.splice(ingredientIndex, 1) } } // Toggle the completed value for a given ingredient const toggleIngredients = (id, recipeId) => { const recipes = getRecipe() const recipe = recipes.find((recipe) => recipe.id === recipeId) const ingredients = recipe.ingredients const ingredient = ingredients.find((ingredient) => ingredient.id === id) if (ingredient) { ingredient.completed = !ingredient.completed saveRecipe() } } // render the ingredients const renderIngredients = (recipeId) => { const ingredientEl = document.querySelector('#ingredients') const recipes = getRecipe() const recipe = recipes.find((recipe) => recipe.id === recipeId) const ingredients = recipe.ingredients ingredientEl.innerHTML = '' if (ingredients.length > 0) { ingredients.forEach((ingredient) => { ingredientEl.appendChild(generateIngredientDOM(ingredient)) }) } else { const messageEl = document.createElement('p') messageEl.classList.add('empty-message') messageEl.textContent = 'No ingredients to show' ingredientEl.appendChild(messageEl) } saveRecipe() } const generateIngredientDOM = (ingredient) => { const ingredientEl = document.createElement('label') const containerEl = document.createElement('div') const ingredientText = document.createElement('span') const checkbox = document.createElement('input') const removeButton = document.createElement('button') const recipes = getRecipe() const recipe = recipes.find((recipe) => recipe.id) // Setup ingredient checkbox checkbox.setAttribute('type', 'checkbox') checkbox.checked = ingredient.completed containerEl.appendChild(checkbox) checkbox.addEventListener('change', () => { toggleIngredients(ingredient.id, recipe.id) renderIngredients(recipe.id) }) // Setup the ingredient text ingredientText.textContent = ingredient.text containerEl.appendChild(ingredientText) // Setup container ingredientEl.classList.add('list-item') containerEl.classList.add('list-item__container') ingredientEl.appendChild(containerEl) // Setup the remove button removeButton.textContent = 'remove' removeButton.classList.add('button', 'button--text') ingredientEl.appendChild(removeButton) removeButton.addEventListener('click', () => { removeIngredients(ingredient.id, recipe.id) renderIngredients(recipe.id) }) return ingredientEl } const generateSummaryDOM = (incompleteIngredients) => { const summary = document.createElement('h2') const plural_pronoun = incompleteIngredients.length <= 1 ? 'is' : 'are' const plural = incompleteIngredients.length <= 1 ? '' : 's' summary.classList.add('list-title') summary.textContent = `There ${plural_pronoun} ${incompleteIngredients.length} ingredient${plural} missing` return summary } export { createIngredients, renderIngredients, generateSummaryDOM }

if I change the function as following如果我改变功能如下

 const createIngredients = (recipeId, text) => { const recipe = recipes.find((recipe) => recipe.id === recipeId); // Returns undefined if(recipe) recipe.ingredients.push({ id: uuidv4(), text, completed: false }) else console.log("Recipe not found"); saveRecipe() }

then I am getting another error: Uncaught ReferenceError: recipes is not defined at createIngredients (ingredients.js:24) at HTMLFormElement.然后我收到另一个错误:Uncaught ReferenceError: recipes is not defined at createIngredients (ingredients.js:24) at HTMLFormElement。 (edit.js:29) (edit.js:29)

But I don't understand why, since I imported recipes.js to ingredients.js, so it should be defined then.但是我不明白为什么,因为我将 recipes.js 导入到了 ingredients.js 中,所以它应该在那时被定义。

below is recipes.js code下面是 recipes.js 代码

 import uuidv4 from 'uuid/v4' let recipes = [] // Get the saved data and return the value found fot the key 'recipes' in localStorage.getItem('recipes') const loadRecipe = () => { const recipesJSON = localStorage.getItem('recipes') try { return recipesJSON ? JSON.parse(recipesJSON) : [] } catch (e) { return [] } } // Save the recipes to localStorage const saveRecipe = () => { localStorage.setItem('recipes', JSON.stringify(recipes)) } const getRecipe = () => recipes const createRecipe = () => { const id = uuidv4() recipes.push({ id: id, title: '', body: '', ingredients: [] }) saveRecipe() return id } const removeRecipe = (id) => { const recipeIndex = recipes.findIndex((recipe) => recipe.id === id) if (recipeIndex > -1) { recipes.splice(recipeIndex, 1) saveRecipe() } } const updateRecipe = (id, updates) => { const recipe = recipes.find((recipe) => recipe.id === id) if (!recipe) { return } if (typeof updates.title === 'string') { recipe.title = updates.title } if (typeof updates.body === 'string') { recipe.body = updates.body } saveRecipe() return recipe } recipes = loadRecipe() export { getRecipe, createRecipe, removeRecipe, updateRecipe, saveRecipe }

I am currently working on a personal project creating a recipe-app.我目前正在开发一个创建配方应用程序的个人项目。 when I launch a dev-server, I am getting an error: ingredients.js:26 Uncaught TypeError: Cannot read property 'push' of undefined当我启动一个开发服务器时,我收到一个错误:成分.js:26 未捕获的类型错误:无法读取未定义的属性“推送”

I think, there is a problem with recipe.ingredients but when I tried to tweak it, I still couldn't solve it.我想,recipe.ingredients 有问题,但是当我尝试调整它时,我仍然无法解决它。 Could anyone please have a look at my source code, a file is called ingredients.js任何人都可以看看我的源代码,一个文件叫做成分.js

 import uuidv4 from 'uuid/v4' import { getRecipe, saveRecipe } from './recipes' const createIngredients = (recipeId, text) => { const recipes = getRecipe() const recipe = recipes.find((recipe) => recipe.id === recipeId) recipe.ingredients.push({ id: uuidv4(), text, completed: false }) saveRecipe() } const removeIngredients = (id, recipeId) => { const recipes = getRecipe() const recipe = recipes.find((recipe) => recipe.id === recipeId) const ingredients = recipe.ingredients const ingredientIndex = ingredients.find((ingredient) => ingredient.id === id) if (ingredientIndex > -1) { ingredients.splice(ingredientIndex, 1) } } // Toggle the completed value for a given ingredient const toggleIngredients = (id, recipeId) => { const recipes = getRecipe() const recipe = recipes.find((recipe) => recipe.id === recipeId) const ingredients = recipe.ingredients const ingredient = ingredients.find((ingredient) => ingredient.id === id) if (ingredient) { ingredient.completed = !ingredient.completed saveRecipe() } } // render the ingredients const renderIngredients = (recipeId) => { const ingredientEl = document.querySelector('#ingredients') const recipes = getRecipe() const recipe = recipes.find((recipe) => recipe.id === recipeId) const ingredients = recipe.ingredients ingredientEl.innerHTML = '' if (ingredients.length > 0) { ingredients.forEach((ingredient) => { ingredientEl.appendChild(generateIngredientDOM(ingredient)) }) } else { const messageEl = document.createElement('p') messageEl.classList.add('empty-message') messageEl.textContent = 'No ingredients to show' ingredientEl.appendChild(messageEl) } saveRecipe() } const generateIngredientDOM = (ingredient) => { const ingredientEl = document.createElement('label') const containerEl = document.createElement('div') const ingredientText = document.createElement('span') const checkbox = document.createElement('input') const removeButton = document.createElement('button') const recipes = getRecipe() const recipe = recipes.find((recipe) => recipe.id) // Setup ingredient checkbox checkbox.setAttribute('type', 'checkbox') checkbox.checked = ingredient.completed containerEl.appendChild(checkbox) checkbox.addEventListener('change', () => { toggleIngredients(ingredient.id, recipe.id) renderIngredients(recipe.id) }) // Setup the ingredient text ingredientText.textContent = ingredient.text containerEl.appendChild(ingredientText) // Setup container ingredientEl.classList.add('list-item') containerEl.classList.add('list-item__container') ingredientEl.appendChild(containerEl) // Setup the remove button removeButton.textContent = 'remove' removeButton.classList.add('button', 'button--text') ingredientEl.appendChild(removeButton) removeButton.addEventListener('click', () => { removeIngredients(ingredient.id, recipe.id) renderIngredients(recipe.id) }) return ingredientEl } const generateSummaryDOM = (incompleteIngredients) => { const summary = document.createElement('h2') const plural_pronoun = incompleteIngredients.length <= 1 ? 'is' : 'are' const plural = incompleteIngredients.length <= 1 ? '' : 's' summary.classList.add('list-title') summary.textContent = `There ${plural_pronoun} ${incompleteIngredients.length} ingredient${plural} missing` return summary } export { createIngredients, renderIngredients, generateSummaryDOM }

if I change the function as following如果我改变功能如下

 const createIngredients = (recipeId, text) => { const recipe = recipes.find((recipe) => recipe.id === recipeId); // Returns undefined if(recipe) recipe.ingredients.push({ id: uuidv4(), text, completed: false }) else console.log("Recipe not found"); saveRecipe() }

then I am getting another error: Uncaught ReferenceError: recipes is not defined at createIngredients (ingredients.js:24) at HTMLFormElement.然后我收到另一个错误:Uncaught ReferenceError: recipes is not defined at createIngredients (ingredients.js:24) at HTMLFormElement。 (edit.js:29) (edit.js:29)

But I don't understand why, since I imported recipes.js to ingredients.js, so it should be defined then.但是我不明白为什么,因为我将 recipes.js 导入到了 ingredients.js 中,所以它应该在那时被定义。

below is recipes.js code下面是 recipes.js 代码

 import uuidv4 from 'uuid/v4' let recipes = [] // Get the saved data and return the value found fot the key 'recipes' in localStorage.getItem('recipes') const loadRecipe = () => { const recipesJSON = localStorage.getItem('recipes') try { return recipesJSON ? JSON.parse(recipesJSON) : [] } catch (e) { return [] } } // Save the recipes to localStorage const saveRecipe = () => { localStorage.setItem('recipes', JSON.stringify(recipes)) } const getRecipe = () => recipes const createRecipe = () => { const id = uuidv4() recipes.push({ id: id, title: '', body: '', ingredients: [] }) saveRecipe() return id } const removeRecipe = (id) => { const recipeIndex = recipes.findIndex((recipe) => recipe.id === id) if (recipeIndex > -1) { recipes.splice(recipeIndex, 1) saveRecipe() } } const updateRecipe = (id, updates) => { const recipe = recipes.find((recipe) => recipe.id === id) if (!recipe) { return } if (typeof updates.title === 'string') { recipe.title = updates.title } if (typeof updates.body === 'string') { recipe.body = updates.body } saveRecipe() return recipe } recipes = loadRecipe() export { getRecipe, createRecipe, removeRecipe, updateRecipe, saveRecipe }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 运行此程序后出现错误,Uncaught TypeError:无法读取未定义的属性“ push” - I am getting error after run this program, Uncaught TypeError: Cannot read property 'push' of undefined 我收到错误 Typerror 无法读取未定义的属性“执行” - I am getting an error Typerror Cannot read property 'execute' of undefined 我在控制台中收到此错误:无法读取未定义的属性“ some” - I am getting this error in the console : Cannot read property 'some' of undefined 为什么我越来越无法读取未定义错误的属性? - why I am getting, cannot read property of undefined error? 我收到错误“无法读取未定义的属性“用户名”” - I am getting the error "cannot read property "username" of undefined" 为什么我得到&#39;无法读取属性&#39;推送&#39;未定义&#39;(React Native)? - Why am I getting 'Cannot read property 'push' of undefined' (React Native)? 为什么我会收到 TypeError:无法读取未定义的 react-router-dom 的属性“push” - Why am I getting TypeError: Cannot read property 'push' of undefined react-router-dom 当我可以打印未定义的变量时,为什么会出现“无法读取未定义错误的属性”? - Why Am I Getting “Cannot read property of undefined error” When I Can Print the Undefined Variable? 在 TypeError 上出现错误:无法读取未定义的属性“推送” - Getting error on TypeError: Cannot read property 'push' of undefined 为什么会收到此错误类型错误:无法读取未定义的属性“客户端” - Why am getting this error TypeError: Cannot read property 'client' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM