prismicio / javascript-kit

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

Dealing with optional attribute #78

Closed ggrossetie closed 6 years ago

ggrossetie commented 9 years ago

Hi,

I have some optional attributes in my document. I'm using Node.js/Express with ejs template:

document.get("page.catch_phrase").asText()

The problem is that I need to check that document.get("page.catch_phrase") != null before calling asText(). Something like:

<% if (document.get("page.catch_phrase") != null ) { %>
<%- document.get("page.catch_phrase").asText() %>
<% } %>

This is really verbose and I need to be extra careful because if I forget one of them my template will return a HTTP 500 :cry: Can we add a shortcut method to handle optional attribute ?

document.getAsText("page.catch_phrase") // corresponding text value or empty ""

Thanks

erwan commented 9 years ago

Hi,

Yes, I agree null-checks are kind of a pain. We need to find the right way to do it, I'm not sure yet what it is.

ggrossetie commented 9 years ago

OK let me know if I can help.

Guillaume Le 8 déc. 2014 23:33, "Erwan Loisant" notifications@github.com a écrit :

Hi,

Yes, I agree null-checks are kind of a pain. We need to find the right way to do it, I'm not sure yet what it is.

— Reply to this email directly or view it on GitHub https://github.com/prismicio/javascript-kit/issues/78#issuecomment-66200888 .

mattfysh commented 7 years ago

@erwan - any thoughts on this?

erwan commented 7 years ago

Hi @mattfysh ,

I'm no longer involved in this project, but @arnaudlewis will probably follow up from here ;)

levimykel commented 6 years ago

This issue is resolved by using the prismic.io API v2 with the prismic-javascript and prismic-dom kits. We recommend updating to this setup to avoid this issue.