0

I've created my function that will fetch for my API to change the user's pace. In my changePace function, i've established my parameter pace, and that parameter will vary depending on which pace is selected by the user. I have 4 paces, Steady, Strenuous, Grueling and Resting. Any of which could be my parameter for the changePace function.

But when I try to call my changePace function with the appropriate parameter, I'm getting the following error.

trail.js:8 Uncaught ReferenceError: steady is not defined
    at HTMLBodyElement.document.body.onkeyup 

JS FILE :

document.body.onkeyup = function(e){
    if(e.keyCode == 13){
        document.getElementById("paces").style.color = "white";
        paceDiv = true;
        console.log("Works");
    }
    if(e.keyCode == 49 && paceDiv == true) {
      changePace(steady);
      document.getElementById("paces").style.color = "black";
    }
    if(e.keyCode == 50 && paceDiv == true){
      changePace(strenuous);
      document.getElementById("paces").style.color = "black";
    }
    if(e.keyCode == 51 && paceDiv == true){
      changePace(grueling);
      document.getElementById("paces").style.color = "black";
    }
    if(e.keyCode == 52 && paceDiv == true){
      changePace(resting);
      document.getElementById("paces").style.color = "black";
    }
    if(e.keyCode == 32) {
      changeDay();
      document.getElementById("paces").style.color = "black";
    }
}

function changePace(pace) {
    fetch('/api/changePace',
        {method: "post",
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json'
            },
            body: '{"pace": "' + pace + '"}'
        })
    .then(function(response) {
        if (response.status !== 200) {
            console.log('Error: ' + response.status + "..." + response.value);
            return;
        }
        response.json().then(function(data) {
            changeDay();
        });
    });
}

1 Answers1

1

I believe you intended steady to be a string, but you've written it as a variable, which you never defined.

Try changePace('steady'); instead.

Or, alternatively, you could define the variables somewhere, maybe near the top of the file. This is the recommended approach if you're going to use the 'steady' string in more than one place.

var steady = 'steady';
var strenuous = 'strenuous';
var grueling = 'grueling';
var resting = 'resting';
Benjamin
  • 1,372
  • 2
  • 12
  • 20