[英]Uncaught TypeError: Cannot read property 'object' of undefined
所以這是我的第一個問題,我是新來的。 所以我正在參加 Udemy 的 javascript 課程,這是我的問題:
我在執行函數之前定義了一個參數(對象),但瀏覽器在它上面拋出了一個錯誤。 所以我在執行函數之前對它進行 console.log 並返回它假設的數據,但是在函數中使用時它會錯誤地指出它是未定義的。
在對試圖找出錯誤失去希望之后,我什至從課程中復制了原始程序中的粘貼代碼,但它仍然會拋出錯誤! 所以我認為這不是代碼的問題,而是其他問題。 也許你知道什么? 無論如何,這是代碼。
const controlList = () => {
// Create a new list IF there in none yet
if (!state.list) state.list = new List();
// Add each ingredient to the list and UI
console.log(state.recipe.ingredients);
state.recipe.ingredients.forEach(el => {
const item = state.list.addItem(el.count, el.unit, el.ingredient);
console.log(state.list.items);
console.log(item); // HERE IS OK
listView.renderItem(item); // THIS LINE THROWS MISTAKE
});
}
export const renderItem = item => {
const markup = `
<li class="shopping__item" data-itemid=${item.id}>
<div class="shopping__count">
<input type="number" value="${item.count}" step="${item.count}" class="shopping__count-value">
<p>${item.unit}</p>
</div>
<p class="shopping__description">${item.ingredient}</p>
<button class="shopping__delete btn-tiny">
<svg>
<use href="img/icons.svg#icon-circle-with-cross"></use>
</svg>
</button>
</li>
`;
elements.shopping.insertAdjacentHTML('beforeend', markup);
};
// controlList() 在這里的事件監聽器中被調用
elements.recipe.addEventListener('click', e => {
if(e.target.matches('.btn-decrease, .btn-decrease *')) {
// Decrrease button is clicked
if (state.recipe.servings > 1) {
state.recipe.updateServings('dec');
recipeView.updateServingsIngredients(state.recipe);
}
} else if (e.target.matches('.btn-increase, .btn-increase *')) {
// Increase button is clicked
state.recipe.updateServings('inc');
recipeView.updateServingsIngredients(state.recipe);
} else if (e.target.matches('.recipe__btn--add, .recipe__btn--add *')) {
// Add ingredients to shopping list
controlList(); <---------------- HERE
}
});
神聖的莫莉 guakamolly 我發現出了什么問題。 從另一個文件導入函數時結果我沒有正確導入......(我的意思是一切*)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.