Discussion about issues retrieving data from Sanity.io in NuxtJS component

20 replies
Last updated: Nov 3, 2021
Hi people 🙂I've been struggling with these for some days now, hopefully you have a simple answer for this
--------
I'm using
Sanity.io with NuxtJS and in a component I'm calling
async fetch()
to retrieve data from the Sanity clientThe following is the query I'm executing

{
  "keepers": *[_type == "keeper"]
}
It gets executed but it returns 0 elements
I executed the same query on Vision and by putting the same url the Sanity client generated in the browser: both give me results
What am I missing?
Nov 3, 2021, 9:24 AM
Hi, can you share your client code?Does it look something like?

import sanityClient from '@sanity/client'

const client = new sanityClient({/* options */})

const main = async () => {
  const result = await client.fetch(`{
    "keepers": *[_type == "keeper"]
  }`)
}

main()
Nov 3, 2021, 10:16 AM
Hello, thank you for answering me 🙂here's the code

<script>
import groq from "groq";
import sanityClient from "~/sanityClient";

const query = groq`{
  "keepers": *[_type == "keeper"]
}`;

export default {
  async fetch() {
    var data = await sanityClient.fetch(query);
    return data;
  },
};
</script>
Nov 3, 2021, 10:57 AM
Hum it seems ok to me. Do you have multiple dataset or only one?
Nov 3, 2021, 11:01 AM
only one
Nov 3, 2021, 11:02 AM
I'm sorry can't find what's missing so 😕
Nov 3, 2021, 11:04 AM
thank you anyway
Nov 3, 2021, 11:06 AM
could it be possible that your documents are all drafts & you didn’t put a API token for your front end?
Nov 3, 2021, 2:28 PM
oh god, this is a bit embarrassing 😄 thank you!
Nov 3, 2021, 2:31 PM
not at all — handling drafts is a bit unintuitive at first with Sanity, since they’re treated as regular documents! 😅
Nov 3, 2021, 2:33 PM
why would the queries work though? I can understand the one in Vision but why executing the url on the browser would work? it's the same action executed by the client!
Nov 3, 2021, 2:33 PM
when you paste the url into the browser, I think it might have used your stored credential. I’m not sure though — could you try pasting it in a igconito window & see if it still works?
Nov 3, 2021, 2:34 PM
sure, will do right now
Nov 3, 2021, 2:34 PM
not at all — handling drafts is a bit unintuitive at first with Sanity, since they’re treated as regular documents!
maybe it should be clearer it's a draft and not yet published
Nov 3, 2021, 2:34 PM
ah well, there's published at the bottom... ok, my bad 🙂
Nov 3, 2021, 2:35 PM
when you paste the url into the browser, I think it might have used your stored credential. I’m not sure though — could you try pasting it in a igconito window & see if it still works?
And you're right again, kind sir 🙂 thank you so much!
Nov 3, 2021, 2:36 PM
maybe it's a stupid question but it might help others if this was documented... could have saved me some time too 🙂
Nov 3, 2021, 2:37 PM
not a stupid question at all — cookie browser sometimes is still magic to me. Glad that you resolved the issue!
Nov 3, 2021, 2:38 PM
no, I mean even the fact that Vision and the browser might show different results than the app when querying data due to documents being drafts
Nov 3, 2021, 2:40 PM
ah, right, that’s tricky for sure.
Nov 3, 2021, 2:41 PM
thank you so much mate 🙂
Nov 3, 2021, 2:50 PM

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?