Using the sanity client to fetch a field from another document and importing sanity.json file.

4 replies
Last updated: Mar 9, 2021
Hello again!
I am trying to use the sanity client to fetch a field from another document while inside a publish action. To get the required api configuration information, I am trying to import the sanity.json file into my action. I have the following code:


import {useState, useEffect} from 'react'
import {useDocumentOperation} from '@sanity/react-hooks'
import sanityClient from '@sanity/client'
import sanityConfig from '../../sanity.json'

export default function SetSlugAndPublishAction(props) {
...

const client = sanityClient({
        projectId: sanityConfig.api.projectId,
        dataset: sanityConfig.api.dataset,
        token: sanityConfig.api.token,
        useCdn: true
      })
And I am seeing the following error:

Compiling...
Failed to compile.
Error in ./sanity.json
Syntax error: /Users/keneucker/Dev/biketag-sanity/studio/sanity.json: Missing semicolon (2:8)
1 | {
> 2 | "root": true,
I assume, from my limited googling, that the issue here is that the plugin
json-loader
may not be included in the project. I went digging for compilation config files to see where I could include this, but I done got myself lost now. 🧑‍💻
Is this breaking some convention in place that I should be aware of? Am I just doing it wrong? Do I need to add
json-loader
and if so, how do I do that?
Thank you!
Mar 9, 2021, 4:24 PM
Hi User. I’ve usually seen people importing their client options via env variables rather than from JSON. I’m intrigued to hear if there’s a reason you’re going that route, though.
Mar 9, 2021, 4:37 PM
Ha. Well. The data was right there and so I went for it. Environment variables is a much better route, I'll jump into that now with this link:

https://www.sanity.io/docs/studio-environment-variables
Mar 9, 2021, 4:38 PM
user A
, thank you so much for your help this week!
I have resolved this, and all of the other issues that I had encountered this week while achieving my goals with creating my first sanity dataset and I'm excited to begin building out my first client!
Mar 9, 2021, 5:07 PM
My pleasure, User! I look forward to seeing some of the awesome stuff you’re building. 👍
Mar 9, 2021, 5:11 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?