简体   繁体   English

JavaScript If语句+未捕获的TypeError

[英]JavaScript If Statement + Uncaught TypeError

1. I want ratingClass = 'fresh' if rating > 59 OR if audience_rating > 70 . 1. ratingClass = 'fresh' if rating > 59 OR if audience_rating > 70我希望ratingClass = 'fresh' if rating > 59 OR if audience_rating > 70 How? 怎么样? I tried with 我尝试过

if (rating > 59) || 如果(评分> 59)|| (audience_rating > 70) { (受众评分> 70){

  var ratingClass = 'fresh'; 

Here's the code: 这是代码:

 if (rating > 59) {
    var ratingClass = 'fresh';
 } else if (rating > 0){
    var ratingClass = 'rotten';
 } else {
   var ratingClass = 'na';
 }
 if (audience_rating > 59) {
    var audienceClass = 'fresh';
 } else if (audience_rating > 0){
    var audienceClass = 'rotten';
 } else {
    var audienceClass = 'na';
 }
 $parentEl.addClass(ratingClass);

2. In line 114 of http://pastebin.com/UN8wcB7b I get Uncaught TypeError: Cannot read property 'length' of undefined every ~3 seconds when hideRotten = true. 2.http://pastebin.com/UN8wcB7b的第114行中,我得到了Uncaught TypeError:当hideRotten = true时,无法每隔约3秒读取未定义的属性“ length”。 Is it easily fixed and/or do I need to worry about it at all? 它很容易修复和/或我根本不需要担心吗?

I am new to JavaScript coding, currently I am trying to learn by doing. 我是JavaScript编码的新手,目前我正在尝试边做边学。 Can you recommend any resources to learn writing Chrome Extensions with JavaScript? 您可以推荐任何资源来学习用JavaScript编写Chrome扩展程序吗?

Thanks :-) 谢谢 :-)

it's because you are trying to read the length of a null element. 这是因为您尝试读取null元素的长度。 So put a condition to test if movies are null in your if statement on line 114 so it says something like 因此,在第114行的if语句中放置一个条件来测试电影是否为空,因此它会显示类似

if(data.movies && data.movies.length > 0)

Though if you're setting some data in this if statement that will be used other places in the code you may have to put checks like this in other places as well to completely avoid this type of problems. 虽然如果您要在此if语句中设置一些数据,这些数据将在代码的其他地方使用,则可能也必须在其他地方进行这样的检查,以完全避免此类问题。

1) the condition after the if must always be completely surrounded in brackets: 1) if之后的if必须始终完全用括号括起来:

// wrong
if (rating > 59) || (audience_rating > 70) {

// has to be:
if ( rating > 59  || audience_rating > 70 ) {

or if you are unsure about the operator precedence : 或者,如果您不确定运算符的优先级

if ( (rating > 59)  || (audience_rating > 70) ) {

2) You have to check first, if the movies attribute exists in your data respone (Because if it doesn't, you also can't call length on it): 2)您必须先检查,如果movies属性存在于您的data响应中(因为如果不存在,那么您也不能对其调用长度):

// can throw error if data.movies === undefined
data.movies.length > 0

// the safe way, check data.movies first:
if (data.movies && data.movies.length > 0)

this is pretty much equivalent to the long version * : 这几乎等效于长版本*

if (typeof(data.movies) === `undefined` && data.movies.length > 0)

* Not exactly, read this article why * 不完全是,阅读这篇文章为什么

The error definitely means 该错误肯定意味着

typeof data.movies === "undefined"

to avoid this I will recommend 为了避免这种情况,我会建议

... ...

$.getJSON(movieUrl, function(data){
    // data can be undefined becoz of various reasons and so is data.movies
    if(!(typeof data === "undefined") && !(typeof data.movies === "undefined")) {
    //put similar checks in ur code

... ...

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

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