Streaming live at 10am (PST)

How can I read collection data in custom code?

To start with, I’m just trying to be able to read an id from a collection item in javascript custom code block so I can make an api request with it. Is there no api to do this? Is there no way to access your cms data in custom code? I could probably use that id as a slug and read that from the url but that isn’t a long term solution for me. Thanks.

EDIT To expand on this, I’d also like to be able to read a number of coordinates from the cms and display them using mapbox.

Hey there!

Not sure if this is completely what you’re looking for, but you can display the contents of a CMS field via the HTML embed field when it is inserted on a CMS collection page.

You can access the data fields by clicking the “Add Field” link in the top right hand corner of the code editor, and then selecting from the dropdown which data you would like to insert. Then the selected field would show up within your code as “Name” does in my example (of course it would be replaced with the corresponding data on the actual site.

One example of how I have used this on a site is that I parsed the data from the collection list into a “hidden field” within a hidden field to see what page a user was on before they were redirected to the form.

Hope this helps! :slight_smile:

Yeah I was hoping for something like this but available in the ‘Before Body’ code block in a pages settings. Or even better some kind of globally available accessor so I could just do something like ‘collections(‘posts’).find({id: 1})’. Otherwise your solution looks like the only way. I’m still getting used to this ‘no-code’ mentality. Thanks!

A ton of us want this, too! We’ve used methods similar to the suggestion from @TylerCourts, but this is something we’ll bring up again internally to the teams working in this area. Native support for injecting or otherwise referencing CMS data to make it readable by the browser (and other services) would be absolute magic.

1 Like