4

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 combination 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!

4
  • What happens if data, flightStatuses, operationalTimes or publishedDeparture are also undefined? 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 () Commented Sep 10, 2013 at 15:22
  • OK. GREAT answers. I'll try a couple of these approaches and report back! thanks. 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..... } Commented Sep 10, 2013 at 16:01

3 Answers 3

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
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

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

1 Comment

"? true : false" not needed
0

Could this work?

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.