javascript - Uncaught TypeError: Can not read property 'odd' of undefined

0

Hello

In my JavaScript code I get the following error:

  

"Uncaught TypeError: Can not read property 'odd' of undefined"

Since I'm new to the area, I do not understand how to solve the problem, can you help me?

case 'multiple_2_3':

    total = 3 * self.card.quantity;
    $("#resume-odds").hide();
    //gains = (cota1 * cota2) * self.card.quantity + (cota1 * cota3) * self.card.quantity +  (cota2 * cota3) * self.card.quantity
    for (let i in self.card.bets) {
        let bet1 = self.card.bets[i];
        if(self.card.bets.length > i){
        for (let j = i + 1; j < self.card.bets.length; j++) {
            let bet2 = self.card.bets[j];
            gains += parseFloat(bet1.odd) * parseFloat(bet2.odd) * self.card.quantity;
        }
        }
    }

    break;
case 'multiple_2_4':
    total = 6 * self.card.quantity;
    $("#resume-odds").hide();

    for (let i in self.card.bets) {
        let bet1 = self.card.bets[i];
        for (let j = i + 1; j < self.card.bets.length; j++) {
            let bet2 = self.card.bets[j];
            gains += parseFloat(bet1.odd) * parseFloat(bet2.odd) * self.card.quantity;
        }
    }

    break;
    
asked by anonymous 12.07.2018 / 11:42

1 answer

1

This error means that bet1 has no set value. That is, when doing let bet1 = self.card.bets[i]; the object or array .bets has no value for property i (or has value and it is undefined ).

Since you have if(self.card.bets.length > i){ before, this ensures that i is not greater than array, which is fine, but indicates that the i position of this array is undefined . That's where you have to look for the problem and figure out why it happened. This will already be on another question maybe but see how you construct this array and where the values come from.

    
12.07.2018 / 11:55