[英]Uncaught TypeError: Cannot read property 'object' of undefined
So this is my first ever question and I'm new here.所以这是我的第一个问题,我是新来的。 So I'm attending course on Udemy for javascript and this is my problem:
所以我正在参加 Udemy 的 javascript 课程,这是我的问题:
I define a parameter(object) before executing a function but the browser throws an error on it.我在执行函数之前定义了一个参数(对象),但浏览器在它上面抛出了一个错误。 So I console.log it before executing function and it returns the data like it suppose to be, but when using in a function it errors out that it's undefined.
所以我在执行函数之前对它进行 console.log 并返回它假设的数据,但是在函数中使用时它会错误地指出它是未定义的。
After losing hope at trying to find mistake I even copy paste code from the original program from the course but it still throws mistake!在对试图找出错误失去希望之后,我什至从课程中复制了原始程序中的粘贴代码,但它仍然会抛出错误! So I think it's not a problem with the code but something else.
所以我认为这不是代码的问题,而是其他问题。 Maybe you know what?
也许你知道什么? Here is the code anyway.
无论如何,这是代码。
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() is called in the event listener here // 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
}
}); });
Holy molly guakamolly I found whats wrong.神圣的莫莉 guakamolly 我发现出了什么问题。 Turn out while importing functions from another file I didn't import correctly... (I mean everything *)
从另一个文件导入函数时结果我没有正确导入......(我的意思是一切*)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.