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)
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);
}
};
document.querySelector(".playercards").addEventListener("click",clickedOnCard, false);