Sempre errore durante il tentativo di ottenere un titolo dalla struttura

0

Domanda

ho bisogno di aiuto, ricevo questo errore durante il tentativo di ottenere un titolo dalla struttura, non so come risolvere il problema.

Questo codice funziona bene.


    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'));

Ma questo errore si verifica quando si utilizza questo codice deck[cardId].title Non funziona, non so perché, ho provato a mettere solo un numero come questo deck[1].title ma ancora mi viene un messaggio di errore. Che problema potrebbe essere?

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

Errore di immagine

47 linea è


    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

Migliore risposta

0

è necessario scorrere l'array per ottenere il vostro articolo, si prega di utilizzare la funzione trova

2021-11-22 16:30:32

Sto solo cercando di ottenere l'id dell'elemento html (pulsante) document.querySelector(".playercards").addEventListener("click",clickedOnCard, false);
Man Tas

codeSandboxwould essere più utile, non sembra molto chiaro
Anshuk

Questo è tutto il mio codice, non so come utilizzare codeSandbox, mi dispiace codesandbox.io/s/youthful-darkness-dcvok?file=/gamescript.js
Man Tas

In altre lingue

Questa pagina è in altre lingue

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