user3762201
user3762201

Reputation: 1

How to find and fix a type error of undefined property

I'm following a tutorial on a crossword puzzle but after I trigger the selectLetter() function by pressing a key I'm getting this message when I run it in chrome:

"Uncaught TypeError: Cannot read property 'clueA' of undefined"*

and this message when I run it in firefox developer:

"TypeError: currentLetter.dataset is undefined".*

I tried using typeof in console.log to track the values of currentLetter and currentLetter.dataset but that's just confused me more.

var allLetters;
var currentLetter;
var wordLetters;
var acrossClue;
var downClue;
var typeDirection = "right";
window.onload = init;

function init() {
  allLetters = document.querySelectorAll("table#crossword span");
  currentLetter = allLetters[0];
  var acrossID = currentLetter.dataset.clueA;
  var downID = currentLetter.dataset.clueD;
  acrossClue = document.getElementById(acrossID);
  downClue = document.getElementById(downID);
  formatPuzzle(currentLetter);
  for (var i = 0; i < allLetters.length; i++) {
    allLetters[i].style.cursor = "pointer";
    allLetters[i].onmousedown = function(e) {
      formatPuzzle(e.target);
    }
  }
  document.onkeydown = selectLetter;
  console.log("----init function----\n" + "Current Letter Type: " + typeof currentLetter + " \ncurrentLetter.dataset Type: " + typeof currentLetter.dataset);
}


function formatPuzzle(puzzleLetter) {
  currentLetter = puzzleLetter;
  for (var i = 0; i < allLetters.length; i++) {
    allLetters[i].style.backgroundColor = "";
  }
  acrossClue.style.color = "";
  downClue.style.color = "";

  if (currentLetter.dataset.clueA !== undefined) {
    acrossClue = document.getElementById(currentLetter.dataset.clueA);
    acrossClue.style.color = "blue";
    wordLetters = document.querySelectorAll("[data-clue-a=" + currentLetter.dataset.clueA + "]");
    for (i = 0; i < wordLetters.length; i++) {
      wordLetters[i].style.backgroundColor = "rgb(231,231,255)";
    }
  }
  if (currentLetter.dataset.clueD !== undefined) {
    downClue = document.getElementById(currentLetter.dataset.clueD);
    downClue.style.color = "red";
    wordLetters = document.querySelectorAll("[data-clue-d=" + currentLetter.dataset.clueD + "]");
    for (i = 0; i < wordLetters.length; i++) {
      wordLetters[i].style.backgroundColor = "rgb(255,231,231)";
    }
  }
  if (typeDirection === "right") {
    currentLetter.style.backgroundColor = "rgb(191,191,255)";
  } else {
    currentLetter.style.backgroundColor = "rgb(255,191,191)";
  }
  console.log("----formatPuzzle function----\n" + "Current Letter Type: " + typeof currentLetter + " \ncurrentLetter.dataset Type: " + typeof currentLetter.dataset);
}


function selectLetter(e) {
  var leftLetter = currentLetter.dataset.left;
  var upLetter = currentLetter.dataset.up;
  var rightLetter = currentLetter.dataset.right;
  var downLetter = currentLetter.dataset.down;
  var userKey = e.keyCode;
  console.log("----selectLetter function----\n" + "Current Letter Type: " + typeof currentLetter + " \ncurrentLetter.dataset Type: " + typeof currentLetter.dataset + "\n.dataset.up:" + currentLetter.dataset.up + "\nkeycode:" + userKey);
  if (userKey === 37) {
    formatPuzzle(leftLetter);
  } else if (userKey === 38) {
    formatPuzzle(upLetter);
  } else if (userKey === 39) {
    formatPuzzle(rightLetter);
  } else if ((userKey === 40) || (userKey === 13)) {
    formatPuzzle(downLetter);
  } else if ((userKey === 8) || (userKey === 46)) {
    currentLetter.textContent = "";
  } else if (userKey === 32) {
    switchTypeDirection;
  } else if (65 <= userKey <= 90) {
    currentLetter.textContent = getChar(userKey);
    if (typeDirection === "right") {
      formatPuzzle(rightLetter);
    } else {
      formatPuzzle(downLetter);
    }
  }
  e.preventDefault();
}

function switchTypeDirection() {

}

function getChar(keyNum) {
  return String.fromCharCode(keyNum);
}
<table id="crossword">
  <caption>Today's Crossword</caption>
  <tr>
    <td><span id="c1_1" data-letter="C" data-right="c1_2" data-left="c1_8" data-down="c2_1" data-up="c11_1" data-clue-a="a1" data-clue-d="d1"></span><sup>1</sup></td>
    <td><span id="c1_2" data-letter="A" data-right="c1_3" data-left="c1_1" data-down="c2_2" data-up="c11_2" data-clue-a="a1" data-clue-d="d2"></span><sup>2</sup></td>
    <td><span id="c1_3" data-letter="M" data-right="c1_4" data-left="c1_2" data-down="c2_3" data-up="c11_3" data-clue-a="a1" data-clue-d="d3"></span><sup>3</sup></td>
    <td><span id="c1_4" data-letter="O" data-right="c1_5" data-left="c1_3" data-down="c2_4" data-up="c10_4" data-clue-a="a1" data-clue-d="d4"></span><sup>4</sup></td>
    <td><span id="c1_5" data-letter="M" data-right="c1_6" data-left="c1_4" data-down="c2_5" data-up="c11_5" data-clue-a="a1" data-clue-d="d5"></span><sup>5</sup></td>
    <td><span id="c1_6" data-letter="I" data-right="c1_7" data-left="c1_5" data-down="c2_6" data-up="c11_6" data-clue-a="a1" data-clue-d="d6"></span><sup>6</sup></td>
    <td><span id="c1_7" data-letter="L" data-right="c1_8" data-left="c1_6" data-down="c2_7" data-up="c11_7" data-clue-a="a1" data-clue-d="d7"></span><sup>7</sup></td>
    <td><span id="c1_8" data-letter="E" data-right="c1_1" data-left="c1_7" data-down="c2_8" data-up="c11_8" data-clue-a="a1" data-clue-d="d8"></span><sup>8</sup></td>
    <td class="blank"></td>
  </tr>

I expect the arrow keys to change the background color rgb(191,191,255) of the cell the arrow key directs to in the puzzle but the code stops running and a type error is printed to the console.

Upvotes: 0

Views: 234

Answers (1)

Felix Kling
Felix Kling

Reputation: 816364

The error implies that currentLetter is assigned a value that is not a DOM element (because every DOM element has a dataset property).

The only places you assign to currentLetter is in init:

currentLetter = allLetters[0];

and formatPuzzle:

currentLetter = puzzleLetter;

allLetters[0] will be a DOM element.

puzzleLetter is the parameter of formatPuzzle, so lets see whether the function is called and passed something that is not a DOM element.

In selectLetter you are calling formatPuzzle multiple times with different arguments, but none of them is a DOM element. E.g. leftLetter is the value of currentLetter.dataset.left.

Maybe you meant to do

var leftLetter = documnent.getElementById(currentLetter.dataset.left);
// etc

?


Make sure you are only passing DOM elements to the function, and avoid global variables as much as possible.

Upvotes: 1

Related Questions