✨Discover storytelling in the AI age with Pixar's Matthew Luhn at Sanity Connect, May 8th—register now

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– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?