简体   繁体   English

未捕获的TypeError:无法读取未定义的属性“数量”

[英]Uncaught TypeError: Cannot read property 'quantity' of undefined

I have a function 我有一个功能

var foodArray = [] // this global variable arr contains list of object with property 'name' and 'quantity'. 

function x (){
 $('#menu-list button').click(function(){
//judge whether a button is add or minus
var type = $(this).attr('name');
//find current minus button element
var minus = $(this).closest('.food-edit').find('.minus');
//find current item's sales number element
var nSales = $(this).closest('.food-edit').find('.num-sales');
//conver current item's sales to a number
var num_Sales = Number(nSales.text());

//find food attributes
var foodNameStr = $($(this).closest('.food-detail').find('.food-name')).text();
var foodPrice = $($(this).closest('.food-detail').find('.food-price-num')).text();
var foodPrice_Num = parseFloat(foodPrice);

//take a food ID in DB
var foodID = $(this).closest('.food-detail').attr('id');
var getIndex = FindIt(foodID,foodArray);



if (type == "add") { //add a food into basket
    minus.show();
    TotalOperation(type,foodPrice_Num);
    if ( getIndex == -1) {
        var foodObj = {};
        //add a new object
        foodObj.ID = foodID;
        foodObj.name = foodNameStr;
        foodObj.price = foodPrice_Num;
        foodObj.quantity = 1;
        foodArray.push(foodObj);
        nSales.text("1");
    }
    else {
        EditQuantity(nSales,getIndex);// I get a index from previous code and pass to this function.And before I call this function. The arr already has couple objects for sure.
    }


}

}

function EditQuantity(ns,index){
  arr[index].quantity++;
  ....
}

When the execute to EditQuantity, it gives me a error Uncaught TypeError: Cannot read property 'quantity' of undefined. 当执行EditQuantity时,它给我一个错误Uncaught TypeError:无法读取未定义的属性'quantity'。 But If I put arr[index].quantity++; 但是如果我放arr [index] .quantity ++; directly in the function x, it works fine. 直接在函数x中,它可以正常工作。 When it goes to a helper function it gives such error. 当使用辅助功能时,会出现此类错误。 What do I did wrong? 我做错了什么?

  1. Replace 'arr' with global array 'foodarray' in function 'EditQuantity'. 将函数“ EditQuantity”中的“ arr”替换为全局数组“ foodarray”。
  2. Currently, the elements are added to foodArray only if getindex = -1 which is incorrect. 当前,仅当getindex = -1不正确时,才将元素添加到foodArray。 This will always results in foodArray never having more than 1 element. 这将始终导致foodArray的元素永远不会超过1个。

To fix it, in place of 'else' use if (getindex!=-1) before calling EditQuantity method. 要解决此问题,请在调用EditQuantity方法之前使用if(getindex!=-1)代替'else'。

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

相关问题 未捕获的TypeError:无法读取未定义的属性“未定义” - Uncaught TypeError: Cannot read property 'undefined' of undefined 未捕获的TypeError:无法读取未定义的属性'fromJSON' - Uncaught TypeError: Cannot read property 'fromJSON' of undefined 未捕获的TypeError:无法读取未定义的属性“ timing” - Uncaught TypeError: Cannot read property 'timing' of undefined 未捕获的TypeError:无法读取未定义的属性'formatter' - Uncaught TypeError: Cannot read property 'formatter' of undefined Uncaught TypeError:无法读取未定义的属性“ stopVideo” - Uncaught TypeError: Cannot read property 'stopVideo' of undefined 未捕获的类型错误:无法读取未定义的属性“setCrossOrigin” - Uncaught TypeError: Cannot read property 'setCrossOrigin' of undefined 未捕获的TypeError:无法读取未定义的属性'NaN' - Uncaught TypeError: Cannot read property 'NaN' of undefined 未捕获的TypeError:无法读取未定义的属性'getAttribute' - Uncaught TypeError: Cannot read property 'getAttribute' of undefined 未捕获的TypeError:无法读取未定义的属性“地理编码” - Uncaught TypeError: Cannot read property 'geocode' of undefined 未捕获的TypeError:无法读取未定义的属性“时间戳” - Uncaught TypeError: Cannot read property 'timestamp' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM