未捕获的类型错误:无法读取文本字段上未定义错误的属性“toLowerCase”
Posted
技术标签:
【中文标题】未捕获的类型错误:无法读取文本字段上未定义错误的属性“toLowerCase”【英文标题】:Uncaught TypeError: Cannot read property 'toLowerCase' of undefined Error on text field 【发布时间】:2017-08-23 02:26:57 【问题描述】:未捕获的类型错误:无法读取未定义的属性“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)
在以下代码的注释掉的 if 代码中尝试使用 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
我不确定为什么我会收到描述错误,而不是标题错误。在 if 中使用它之前,我曾尝试在描述中使用 toLowerCase() ,但这不起作用。我只能在 console.logging 时使用它。谁能帮帮我。非常非常感谢!
【问题讨论】:
错误表示description
是undefined
。
【参考方案1】:
您收到错误的原因是您的“标题”或描述等于未定义。以下示例引发了完全相同的异常错误:
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 语句中检查它们是否为空或未定义,如下所示:
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');
;
你看它工作正常。
【讨论】:
你太棒了,这就像一个魅力。我不知道任何数据都是未定义的,但仔细想想,这是一个愚蠢的假设。非常感谢您解决此问题,非常感谢!!!!以上是关于未捕获的类型错误:无法读取文本字段上未定义错误的属性“toLowerCase”的主要内容,如果未能解决你的问题,请参考以下文章
发生以下错误:RGB.js:16未捕获的TypeError:无法读取HTMLButtonElement上未定义的属性'add'。((RGB.js:16)
未捕获的类型错误:无法读取未定义的属性“authenticateClientAndRetrieveSessionId”
反应 - 未捕获的类型错误:无法读取未定义的属性“toLowerCase”