prismicio / javascript-kit

Development kit for the Javascript language
https://developers.prismic.io
106 stars 69 forks source link

Error thrown for `getBoolean` when fragment does not yet exist. #118

Open ctrlplusb opened 8 years ago

ctrlplusb commented 8 years ago
  getBoolean: function(name) {
    var fragment = this.get(name);
    return fragment.value && (fragment.value.toLowerCase() == 'yes' || fragment.value.toLowerCase() == 'on' || fragment.value.toLowerCase() == 'true');
  },

Need to check if fragment is not null?

I had a "Select" type with yes/no, and I show default text of "no". User's then don't select "yes" assuming the value is "no" and then when I try to interpret the value I get the following error:

TypeError: Cannot read property 'value' of null

erwan commented 8 years ago

yes, we need to do a better job with the NPE in general (not just for getBoolean). We're working on a newer version that will improve that.

evvvritt commented 7 years ago

Hi @erwan, any update on this? I vote it returns false if not yes, on, true — so including undefined and null. Thanks

erwan commented 7 years ago

@arnaudlewis I believe you worked on that?

arnaudlewis commented 7 years ago

@erwan no i did not but thanks for your message. I'll take a look asap @evvvritt

sarunast commented 7 years ago

Also if you call api.getLink('text-url').url(). It errors out if it doesn't exist, this shouldn't be the case in my opinion. You could use lodash get method to solve it: https://lodash.com/docs/4.17.4#get. @erwan