gpt4 book ai didi

javascript - 未捕获的类型错误 : Cannot read property 'toLowerCase' of undefined Error on text field

转载 作者:行者123 更新时间:2023-12-03 04:40:53 28 4
gpt4 key购买 nike

Uncaught TypeError: Cannot read property 'toLowerCase' of undefined

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 的结果,表明“biology”确实出现在字符串中)。我什至可以这样说:

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

仍然得到正确的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>

我不知道为什么我收到描述错误而不是标题错误。在 if 中使用 toLowerCase() 之前,我尝试在描述中使用 toLowerCase() ,但这不起作用。我只能在 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');
};

您会发现它运行良好。

关于javascript - 未捕获的类型错误 : Cannot read property 'toLowerCase' of undefined Error on text field,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43096237/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com