【问题标题】:Uncaught TypeError Cannot read property 'toLowerCase' of undefined未捕获的类型错误无法读取未定义的属性“toLowerCase”
【发布时间】:2018-05-16 11:23:38
【问题描述】:

当前从 API 获取数据以创建类似 Jeopardy 的游戏,当我尝试从 API 获取变量并将其传递给 checkUserAnswer 然后继续在其上使用 toLowerCase() 时出现我的问题,我收到该错误从我从其他问题中收集到的信息中,我不知道如何解决这个问题....有什么建议吗?

代码:

var gamePts;

function game(){

    this.start = function(click){

        if(click == true){
            checkUserAnswer();
        }else{
            getQuestion();
        }
    }
    function getQuestion(){
        $.ajax({
            url:"http://jservice.io/api/random",
            dataType: "json"
        })
        //after the ajax call the data is sent to the populateQuestion method.
        .done(function(data){populateQuestion(data);})
        .fail(function(jqXHR, textStatus, errorThrown){
            showError(errorThrown);
        });
    }
    function populateQuestion(data){

        $("#Category").html(data[0].category.title);
        $("#question").html(data[0].question);
        var $correct = (data[0].answer);
        checkUserAnswer($correct);
    }
    function checkUserAnswer(correct){
        //allows the user answer to appear.
        var Correct = this.correct;
        $("#useAns").show();
        var $userGuess = ($('#AnsBx').val()).toLowerCase();
        //var $Correct = correct.toUpperCase();
        //$("#userA").val(userGuess);

        //$("#userA").html(userGuess);
        //$("#userA").html($userGuess);
        $("#Result").show();
        $("#Result").html(Correct);


        if($userGuess === Correct.toLowerCase()){
            $("#userA").html($userGuess)
        }
        else{
            $("#userA").html("wrong");
        }
        // can't figure out why the toUpperCase won't work, getting cannot read property 'toUpperCase' of undefined
    }
    function guessCount(){
        var guesses;

        //if user answer = game anwser 1 try and win = true, print number of tries

        //if user answer != game anwser ct+1 anwser again

        //if user try = 10 end game show answer to question.
    }
}
//was last working on the comparasion to allow the game to be functional.

【问题讨论】:

  • 尝试记录typeof(Correct)
  • 确保你的变量Correct是字符串类型的!

标签: javascript jquery html


【解决方案1】:
this.start = function(click){

    if(click == true){
       //no passing argument.
        checkUserAnswer();
    }else{
        getQuestion();
    }
}

现在您正在调用该方法,但这里正确的变量具有空值。

function checkUserAnswer(correct){
    //allows the user answer to appear.
    var Correct = this.correct;
    //here Correct is null
    $("#useAns").show();

    var $userGuess = ($('#AnsBx').val()).toLowerCase();
      //so null.toLowerCase();uncaught error .
    if($userGuess === Correct.toLowerCase()){
        $("#userA").html($userGuess)
    }
    .......
}

编辑 1:目前我认为的解决方法是硬编码正确的字符串

$("#Result").html("correct");


        if($userGuess === "correct".toLowerCase()){
            $("#userA").html($userGuess)
        }

【讨论】:

  • 我该如何解决这个问题?作业要求一个开始游戏按钮,我创建了一个按钮,用于用户何时回答以及我之前的方式在通过 this.start 函数运行时重新填充问题
猜你喜欢
  • 2014-07-06
  • 2016-03-25
  • 1970-01-01
  • 2021-10-13
  • 2020-11-17
  • 2017-06-26
  • 1970-01-01
  • 2014-07-25
相关资源
最近更新 更多