Join us live Sept. 27 – How Sanity and Vercel powered Morning Brew's transformation –>

Hello everyone, can anyone tell me how to access document data from within the structure builder? ```import client from "@sanity/client" client.getDocument('tags').then(tags...

6 replies
Last updated: Dec 22, 2020

Hello everyone,can anyone tell me how to access document data from within the structure builder?

import client from "@sanity/client"

client.getDocument('tags').then(tags => {
    console.log(`${tags.tagArray}`)
})
i get the error message:
TypeError: _client.default.getDocument is not a function
at Object.<anonymous> (/static/js/app.bundle.js
:180469:17) at __webpack_require__ (/static/js/vendor.bundle.js
:713:30) at fn (/static/js/vendor.bundle.js
:118:20) at loadStructure (/static/js/app.bundle.js
:179724:15) at DeskTool.derivePanes (/static/js/app.bundle.js
:401564:56) at DeskTool.derivePanes (/static/js/app.bundle.js
:441377:25) at DeskTool.componentDidMount (/static/js/app.bundle.js
:401673:10) at DeskTool.componentDidMount (/static/js/app.bundle.js
:441367:123) at commitLifeCycles (/static/js/vendor.bundle.js
:23112:22) at commitLayoutEffects (/static/js/vendor.bundle.js
:26101:7)

Dec 22, 2020, 10:43 AM

The default export of

@sanity/client
is a sanity client constructor, not a singleton. You need to construct a client, then you can access its documents

Dec 22, 2020, 11:07 AM

See the introductory example on how to construct one https://www.sanity.io/docs/js-client

Dec 22, 2020, 11:09 AM

well that helps, thanks a lot

Dec 22, 2020, 11:31 AM

import client from 'part:@sanity/base/client
will do the trick πŸ™‚

Dec 22, 2020, 12:57 PM

import client from 'part:@sanity/base/client
will do the trick πŸ™‚

Dec 22, 2020, 12:57 PM

Here's another example using observables to make the structure real-time https://github.com/sanity-io/sanity-recipes/blob/master/snippets/deskStructureWithRoles.js

Dec 22, 2020, 12:59 PM

Sanity.io: Get the most out of your content

Sanity.io is a platform to build websites and applications. It comes with great APIs that let you treat content like data. Give your team exactly what they need to edit and publish their content with the customizable Sanity Studio. Get real-time collaboration out of the box. Sanity.io comes with a hosted datastore for JSON documents, query languages like GROQ and GraphQL, CDNs, on-demand asset transformations, presentation agnostic rich text, plugins, and much more.

Don't compromise on developer experience. Join thousands of developers and trusted companies and power your content with Sanity.io. Free to get started, pay-as-you-go on all plans.