Získanie chyba pri pokuse získať titul od štruktúry

0

Otázka

potrebujem pomoc, som ako túto chybu pri pokuse získať titul z štruktúru, neviem, ako to opraviť.

Tento kód funguje v pohode.


    function addCard(x,who) {
    
      if(who === 'player'){
        const  element = document.querySelector(".playercards");
        element.insertAdjacentHTML('afterbegin', `<button id='${x}' class='cardbutton'><p>${deck[x].title}</br></br>Action: ${deck[x].points} ${deck[x].action}</p></button>`);
      }
      if(who === 'bot'){
        const  element = document.querySelector(".botcards");
        element.insertAdjacentHTML('afterbegin', `<button id='bot${x}' class='cardbutton'><p>${deck[x].title}</br></br>Action: ${deck[x].points} ${deck[x].action}</p></button>`);
      }
    }
    
    playerCards.forEach((node, position) => addCard(node.getValue(),'player'));
    botCards.forEach((node, position) => addCard(node.getValue(),'bot'));

Ale táto chyba sa stane, keď som sa použiť tento kód deck[cardId].title Nefunguje to, neviem prečo, som sa pokúsil dať len číslo, ako to deck[1].title ale to stále ma dostane chybu. Aký problém by to mohlo byť?

Uncaught TypeError: Cannot read properties of undefined (reading 'title')
    at HTMLUListElement.clickedOnCard (gamescript.js:47)

Chyba obrázok

47 linka je


    console.log(deck[cardId].title())


    const deck = [
      {id: 0, title: 'Šokoladas', action: 'heal', points: 5},
      {id: 1, title: 'Sausainis', action: 'heal', points: 2},
      {id: 2, title: 'Pulti', action: 'damage', points: 3},
      {id: 3, title: 'Saukstas', action: 'damage', points: 1}
    ]

    document.querySelector(".playercards").addEventListener("click",clickedOnCard, false);
    
    function clickedOnCard(e) {
      if(e.target !== e.currentTarget){
        var cardId = e.target.id || e.srcElement.id;
        
        console.log(deck[cardId].title)
    
    
        // Deletes clicked player card
        removeCard(cardId);
    
        // Removes bot card
        removeCard(`bot${botCards.tail().getValue()}`);
        botCards.removeLast();
    
        updateHp();
    
        e.stopPropagation();
    
        postlog(getDate() + " Game: Button clicked." + " Used: " + cardId);
      }
    };

data-structures html javascript
2021-11-22 16:26:46
1

Najlepšiu odpoveď

0

budete musieť opakovať svoje pole, aby si vaše položky, použite, prosím, nájsť

2021-11-22 16:30:32

Ja som len snaží dostať id z html prvku (tlačidlo) document.querySelector(".playercards").addEventListener("click",clickedOnCard, false);
Man Tas

codeSandboxwould byť užitočnejšie, zdá sa, nie je jasné, koľko
Anshuk

To je celý môj kód, neviem, ako používať codeSandbox, sorry codesandbox.io/s/youthful-darkness-dcvok?file=/gamescript.js
Man Tas

V iných jazykoch

Táto stránka je v iných jazykoch

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................