最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Catching undefined javascript variable - Stack Overflow

programmeradmin1浏览0评论

I'm using Ajax/jsonp to access a remote database. As such, the response.error is never returned. I'm trying to catch an instance when the remote server does not return data for whatever reason. I've tried everything I can think of to catch an undefined condition and I can't seem to catch it. I've tried to find the variable with Firebug. I've tried using just about every bination of the following code I can think of and just can't seem to get it to work.

                      if ( typeof(data.flightStatuses[0].operationalTimes.publishedDeparture.dateLocal) === "undefined") {
                        alert("flightstats is undefined");
                    }

Any ideas greatly appreciated!!!!

I also tried:

                      if ( typeof data.flightStatuses === "undefined") {
                        alert("flightstats is undefined");
                    }

Above code won't execute alert either....

FINALLY! This worked...

if ( typeof data.flightStatuses[0] === "undefined")

I don't really know why, but it did. thanks your everyone's help!

I'm using Ajax/jsonp to access a remote database. As such, the response.error is never returned. I'm trying to catch an instance when the remote server does not return data for whatever reason. I've tried everything I can think of to catch an undefined condition and I can't seem to catch it. I've tried to find the variable with Firebug. I've tried using just about every bination of the following code I can think of and just can't seem to get it to work.

                      if ( typeof(data.flightStatuses[0].operationalTimes.publishedDeparture.dateLocal) === "undefined") {
                        alert("flightstats is undefined");
                    }

Any ideas greatly appreciated!!!!

I also tried:

                      if ( typeof data.flightStatuses === "undefined") {
                        alert("flightstats is undefined");
                    }

Above code won't execute alert either....

FINALLY! This worked...

if ( typeof data.flightStatuses[0] === "undefined")

I don't really know why, but it did. thanks your everyone's help!

Share Improve this question edited Sep 10, 2013 at 17:08 hypermiler asked Sep 10, 2013 at 15:18 hypermilerhypermiler 2,1074 gold badges21 silver badges27 bronze badges 4
  • What happens if data, flightStatuses, operationalTimes or publishedDeparture are also undefined? – CodingIntrigue Commented Sep 10, 2013 at 15:20
  • fyi, you don't need typeof unless the leftmost variable may be undefined. in any other case checking for === undefined is pretty safe. Additionally, typeof is an operator, not a function so you can omit the () – ThiefMaster Commented Sep 10, 2013 at 15:22
  • OK. GREAT answers. I'll try a couple of these approaches and report back! thanks. – hypermiler Commented Sep 10, 2013 at 15:54
  • &#^$% OK. I tried: if ( typeof data.flightStatuses === "undefined") { alert("flightstats is undefined"); Still, it won't execute the alert..... } – hypermiler Commented Sep 10, 2013 at 16:01
Add a ment  | 

3 Answers 3

Reset to default 2

If data.flightStatuses is undefined, then data.flightStatuses[0] will throw an error. Make you only check if the relevant identifier is undefined:

if(typeof data.flightStatuses === "undefined") {
    alert("flightStatuses is undefined");
} else {
    // Here you know data.flightStatuses exists, so you can test data.flightStatuses[0]
    if(typeof data.flightStatuses[0] === 'undefined'){
        alert("flightStatuses[0] is undefined");
    } else {
        // And so on, depending on how much you know about your data source
    }
}

This seems to work

   try {
        dateLocal = typeof (data.flightStatuses[0].operationalTimes.publishedDeparture.dateLocal) !== 'undefined';
        if (dateLocal) {
            // Do something with dateLocal
            // ...
        }
    }
    catch (err) {
        alert("flightstats is undefined: " + err);
    }

Could this work?

if (data.flightStatuses[0].operationalTimes.publishedDeparture.dateLocal) === null) {
                        alert("flightstats is undefined");
                    }
发布评论

评论列表(0)

  1. 暂无评论