Issue fetching data using JS API, resolved by making the development dataset public.

6 replies
Last updated: May 30, 2023
Hey there! I'm trying to fetch using the JS api, and nothing is returned in my app, but in the studio stuff is returned.
Code:

  const latestThreeProjects = await client.fetch(
    `*[_type == "project"] | order(_createdAt desc) [0...3]`
  );

// initialized with

import { createClient } from 'next-sanity'

import { apiVersion, dataset, projectId, useCdn } from '../env'

export const client = createClient({
  apiVersion,
  dataset,
  projectId,
  useCdn,
})
API version is 2023-05-25 but I have tried with 2021-10-21.

When I use the same query on the studio "Vision" tab, it works just fine, so I'm not sure what the issue could be here.

Thanks!
May 30, 2023, 1:48 AM
Hi
user G
. Is the dataset private? This could be related to authentication (Vision requests are authenticated via your session in the browser).
May 30, 2023, 2:06 AM
ohhh that's possible, how can I do it within the code?
May 30, 2023, 2:08 AM
It looks like the readme has a fair bit of detail, though not for private datasets. I haven’t used
next-sanity
so I’m not too sure, but I’d start with the section on custom auth . The normal client can take a
token
in its config; it looks like that may be the same here, with the onus on you to ensure the token remains out of your codebase.
May 30, 2023, 2:15 AM
Would it honestly just be easier to use a public dataset? Was only building on a development dataset.
May 30, 2023, 2:25 AM
Nevermind, just made the development dataset public, that made it work. Don't care if any data is shown there anyways haha.
May 30, 2023, 2:27 AM
Great! Glad you got this working.
May 30, 2023, 2:35 AM

Sanity– build remarkable experiences at scale

The Sanity Composable Content Cloud is the 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?