繁体   English   中英

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

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

未捕获的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) 

在以下代码的注释掉的代码中,尝试在说明中使用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));

当我consol.log()时,一切正常(我得到226的结果说“生物学”确实出现在字符串中)。 我什至可以放:

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

并仍能获得<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>

我不确定为什么我得到错误的描述而不是标题。 我曾尝试在描述中使用toLowerCase(),然后在if中使用它,但那没有用。 我只能在console.logging时使用它。 谁能帮我。 非常非常感谢你!

出现错误的原因是当您的“标题”或描述等于未定义时。 以下示例引发了非常相同的异常错误:

 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); }; 

但是,我建议您在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'); }; 

您会看到它工作正常。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM