未捕获的TypeError:无法读取未定义的属性'toLowerCase'似乎找不到错误

时间:2018-07-06 07:31:47

标签: javascript jquery html ajax

我已全局声明的变量:

var audio;
var LANGUAGE;
var audioArray;
var MEDIAARRAY;
var WORDS;
var SOUNDARRAY;

这是导致错误的行:

var audioId = MEDIAARRAY.audio.lowercase.indexOf(exObject['exerciseGetWordInput'].toLowerCase() + '.mp3');

这是完整的功能/ ajax调用:

function setMainObjectArray() {
var exercises = [];
var eBlocks = $('.eBlock');

eBlocks.each(function(i, eBlock) {
    var exObject = {
      word: $(eBlock).find('input.ExerciseGetWordInput').val(),
      audio: '',
      syllables: []
    };

  exObject['exerciseGetWordInput'] = $(this).find('input.exerciseGetWordInput').val();
       var audioId = MEDIAARRAY.audio.lowercase.indexOf(exObject['exerciseGetWordInput'].toLowerCase() + '.mp3');
        var audio_link = '';
           if (audioId > -1) {
               exObject['audio'] = 'https://test.diglin.eu' + MEDIAARRAY.audio.path + MEDIAARRAY.audio.files[audioId];
           }


    $(eBlock).find('input.syllable').each(function(j, syll) {

      exObject.syllables.push($(syll).val());            
    });

    exercises.push(exObject);
  });

return exercises;
}

在其中创建ajax调用以保存它的位置:

function saveExerciseAjaxCall() {
  console.log(setMainObjectArray());


    $.ajax({
        url: 'saveJson.php',
        type: 'POST',
        data: {
            id: getUrlParameter('id'),
            getExerciseTitle: $('#getExerciseTitle').val(),
            language: $('#languageSelector').val(),
            application: 'lettergrepen',
            'main_object': {
              title: $('#getExerciseTitle').val(),
              language: $('#languageSelector').val(),
              exercises: setMainObjectArray()
              },
              dataType: 'json'
            }
    }).done(function(response) {

    }).fail(function(jqXHR, textStatus, errorThrown){
      console.log(jqXHR);
      console.log(errorThrown);
      console.log(textStatus);
    });
}

问题:什么原因导致这种情况发生?为什么?

我的理解:根据该网站:https://teamtreehouse.com/community/uncaught-typeerror-cannot-read-property-tolowercase-of-undefined是因为我正在尝试toLowerCase尚未定义的东西,但是怎么可能呢?我肯定会给exerciseGetWordInput赋值。

应从中获取值的函数:

function getWordInput(id, cValue) {
cValue = cValue || '';
var wInput = $('<input/>', {
    'class': 'exerciseGetWordInput_' + id + ' form-group form-control ExerciseGetWordInput word',  
    'type': 'text',
    'value': cValue,
    'name': 'question_takeAudio_exerciseWord['+ exerciseAudioInput +']',
    'placeholder': 'Exercise',
    'id': 'exerciseGetWordInput',
    'required': true
 });
 return wInput;
}

HTML:

<div  id='my_form' class="container-fluid" method="POST">
   <div class="row"><div class="col-xs-12 col-sm-2"><select name="language" class="languageSelector form-control required" id="languageSelector"></select></div></div>
  <button id='resetInputs' type='button' onclick='getResetInputs()' class='btn btn-danger fa fa-refresh fa-2x resetInputs'></button>
  <button id='saveBtn' class='btn btn-info fa fa-download fa-2x saveBtn' required name="submit" onclick="saveExerciseAjaxCall()"></button>
 </div>

2 个答案:

答案 0 :(得分:1)

这里有一个错字,在indexOf中缺少)

var audioId = MEDIAARRAY.audio.lowercase.indexOf(exObject['exerciseGetWordInput'].toLowerCase() + '.mp3');

尝试:

var audioId = MEDIAARRAY.audio.lowercase.indexOf(exObject['exerciseGetWordInput']).toLowerCase() + '.mp3';

答案 1 :(得分:0)

.toLowerCase()不是lowercase

lowercase不是javascript函数。