简体   繁体   English

未捕获的TypeError:无法在文本字段上读取未定义错误的属性“ toLowerCase”

[英]Uncaught TypeError: Cannot read property 'toLowerCase' of undefined Error on text field

Uncaught TypeError: Cannot read property 'toLowerCase' of undefined 未捕获的TypeError:无法读取未定义的属性“ toLowerCase”

 at Object.<anonymous> (<anonymous>:16:76) at Function.each (jquery.js?v=1.4.4:34) at Object.success (<anonymous>:10:13) at Function.handleSuccess (jquery.js?v=1.4.4:143) at XMLHttpRequest.w.onreadystatechange (jquery.js?v=1.4.4:142) 

I keep getting the above error when trying to use the toLowerCase() function on the description in the commented out if code for the following code: 在以下代码的注释掉的代码中,尝试在说明中使用toLowerCase()函数时,我不断收到上述错误:

(function($) {
    $.ajax({
      url: "/node.json",
      data: {'type': "resource_page", 'page': 3},
      dataType: "json",
      type: "GET",
      success: function (response) {

          $.each(response.list, function(k, resource) {

            var title = resource.title;
            var description = resource.body.value;

              if(title.toLowerCase().indexOf("biology") >= 0) { console.log(description.toLowerCase().indexOf("biology")); };
              //if(title.toLowerCase().indexOf("biology") >= 0 || description.toLowerCase().indexOf("biology") >=0) { console.log(title); };   

          });
      }
    });
}(jQuery));

When I consol.log() it everything works fine (I get the result of 226 saying that "biology" does show up in the string). 当我consol.log()时,一切正常(我得到226的结果说“生物学”确实出现在字符串中)。 I can even put: 我什至可以放:

if(title.toLowerCase().indexOf("biology") >= 0) { console.log(description.toLowerCase()); }; 

and still get the correct console.log of <p>this video database is designed to teach laboratory fundamentals through simple, easy to understand video demonstrations. this collection demonstrates how to execute basic techniques commonly used in cellular and molecular biology. to enhance your understanding of the methods each video is paired with additional video resources to show practical applications of the techniques and other complementary skills.</p> 并仍能获得<p>this video database is designed to teach laboratory fundamentals through simple, easy to understand video demonstrations. this collection demonstrates how to execute basic techniques commonly used in cellular and molecular biology. to enhance your understanding of the methods each video is paired with additional video resources to show practical applications of the techniques and other complementary skills.</p>的正确console.log。 <p>this video database is designed to teach laboratory fundamentals through simple, easy to understand video demonstrations. this collection demonstrates how to execute basic techniques commonly used in cellular and molecular biology. to enhance your understanding of the methods each video is paired with additional video resources to show practical applications of the techniques and other complementary skills.</p> <p>this video database is designed to teach laboratory fundamentals through simple, easy to understand video demonstrations. this collection demonstrates how to execute basic techniques commonly used in cellular and molecular biology. to enhance your understanding of the methods each video is paired with additional video resources to show practical applications of the techniques and other complementary skills.</p>

I am not sure why I get the error for description and not for the title. 我不确定为什么我得到错误的描述而不是标题。 I have tried using toLowerCase() on the description before using it in the if but that did not work. 我曾尝试在描述中使用toLowerCase(),然后在if中使用它,但那没有用。 I can only use it when console.logging it. 我只能在console.logging时使用它。 Can anyone help me please. 谁能帮我。 Thank you very very much! 非常非常感谢你!

The reason that you'are getting the error is when you have either "title" or description equal undefined. 出现错误的原因是当您的“标题”或描述等于未定义时。 The following example throws the very same exception error: 以下示例引发了非常相同的异常错误:

 var title; var description = '<p>this video database is designed to teach laboratory fundamentals through simple, easy to understand video demonstrations. this collection demonstrates how to execute basic techniques commonly used in cellular and molecular biology. to enhance your understanding of the methods each video is paired with additional video resources to show practical applications of the techniques and other complementary skills.</p>'; console.log(description.toLowerCase().indexOf("biology")) if(title.toLowerCase().indexOf("biology") >= 0 || description.toLowerCase().indexOf("biology") >=0) { console.log(title); }; 

However, I would recommend you to check whether they are not null or undefined at your if statement like following: 但是,我建议您在if语句中检查它们是否不为null或未定义,如下所示:

 var title; var description = '<p>this video database is designed to teach laboratory fundamentals through simple, easy to understand video demonstrations. this collection demonstrates how to execute basic techniques commonly used in cellular and molecular biology. to enhance your understanding of the methods each video is paired with additional video resources to show practical applications of the techniques and other complementary skills.</p>'; console.log(description.toLowerCase().indexOf("biology")) if((title && title.toLowerCase().indexOf("biology") >= 0) || (description && description.toLowerCase().indexOf("biology") >=0)) { console.log(title); console.log('It works fine'); }; 

You see that it works fine. 您会看到它工作正常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的TypeError:无法读取属性toLowerCase的未定义 - Uncaught TypeError: Cannot read property toLowerCase of undefined 我有错误未捕获的TypeError:无法读取未定义的属性&#39;toLowerCase&#39; - I have the error Uncaught TypeError: Cannot read property 'toLowerCase' of undefined 未捕获的TypeError:无法读取未定义的属性“ toLowerCase” - Uncaught TypeError: Cannot read property 'toLowerCase' of undefined 未捕获的TypeError无法读取未定义的属性“ toLowerCase” - Uncaught TypeError Cannot read property 'toLowerCase' of undefined 未捕获的TypeError:无法读取未定义的属性'toLowerCase' - Uncaught TypeError: Cannot read property 'toLowerCase' of undefined 获取错误未捕获的TypeError:无法读取未定义的属性“ toLowerCase” - Getting error Uncaught TypeError: Cannot read property 'toLowerCase' of undefined 未捕获的类型错误:无法读取未定义的属性“toLowerCase”(待办事项列表应用程序) - Uncaught TypeError: Cannot read property 'toLowerCase' of undefined (Todo list application ) Elastic_Grid | “未捕获的TypeError:无法读取未定义的属性&#39;toLowerCase&#39;” - Elastic_Grid | “Uncaught TypeError: Cannot read property 'toLowerCase' of undefined” jsonp请求上的“ Uncaught TypeError:无法读取未定义的属性&#39;toLowerCase&#39;” - “Uncaught TypeError: Cannot read property 'toLowerCase' of undefined” on jsonp request TokenInput +未捕获的TypeError:无法读取未定义的属性“ toLowerCase” - TokenInput + Uncaught TypeError: Cannot read property 'toLowerCase' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM