Discussion about configuring Sanity and Nuxt, using the nuxt-sanity module, and resolving issues with fetching data.
40 replies
Last updated: Aug 21, 2020
S
Hiπ, I'm on a project with Sanity + Nuxt. I configured it with in
https://nuxt-sanity.netlify.app/#what-is-sanity and I'm wondering if it's the same or I rather
πThank you for any tip!
import sanityClient from '@sanity/client'
sanity.jsand it works.Now I see this doc
https://nuxt-sanity.netlify.app/#what-is-sanity and I'm wondering if it's the same or I rather
npm install nuxt-sanityAnd in that case: will it overwrite, live with it, or create a conflict with the previous config? I may just try... however if someone already has experience with it ...
πThank you for any tip!
Aug 21, 2020, 7:05 AM
T
I've been working on a new Nuxt module and would love feedback π
Aug 21, 2020, 7:40 AM
T
Aug 21, 2020, 7:40 AM
T
I'm very happy to provide help if the docs aren't clear!
Aug 21, 2020, 7:41 AM
S
user T
it looks very interesting!Aug 21, 2020, 7:55 AM
Hi! Iβve deprecated the
nuxt-sanitypackage,
user T
has done a great job at creating a new and improved one πAug 21, 2020, 7:56 AM
T
And the answer to your question is that it won't conflict with either module, but you'd probably be better off using the client the module creates for ease....
Aug 21, 2020, 8:00 AM
S
Victoria Bergquist
and user T
thank you! this sounds cool!Aug 21, 2020, 8:01 AM
S
I'll go for it π
Aug 21, 2020, 8:07 AM
S
user T
done! and so far all good β¨ ! I will keep you posted with any relevant feedback. thank you!Aug 21, 2020, 8:20 AM
S
Hello
and getting error
I'm clearly missing something. Any hint is very appreciated!
βΊοΈ Thank you!
Victoria Bergquist
and user T
, for usage I'm afraid I need some help. Following https://sanity.nuxtjs.org/usage#fetch my code looks like this:<div class="snippets"> <ul> <li v-for="snippet in snippets" ref="snippet" :key="snippet._id" class="" > <h3>{{ snippet.title }}</h3> </li> </ul> </div> </div> </template> <script> import { groq } from '@nuxtjs/sanity' const query = groq`*[_type == "snippet"][0...29].title` export default { name: 'Snippets', components: {}, async fetch() { const result = await this.$sanity.fetch(query) this.title = result }, data: () => ({ title: '' }), }
Property or method "snippets" is not defined on the instance but referenced during render.
βΊοΈ Thank you!
Aug 21, 2020, 9:54 AM
S
I'm getting as well
Error in fetch(): TypeError: Cannot read property 'fetch' of undefined
Aug 21, 2020, 9:56 AM
T
Do you need to define snippets in your data?
Aug 21, 2020, 10:10 AM
S
this way actually makes more sense to me:
but I still get
asyncData({ $sanity }) { const query = '{ "snippets": *[_type == "snippet"] }' return $sanity.fetch(query) },
Cannot read property 'fetch' of undefined
Aug 21, 2020, 10:10 AM
T
Have you added the module in your nuxt config?
Aug 21, 2020, 10:10 AM
S
Yes I did π
Aug 21, 2020, 10:11 AM
S
in buildModules: ['@nuxtjs/sanity'],
Aug 21, 2020, 10:11 AM
T
Well, the main thing is first to define snippets on your component.
Aug 21, 2020, 10:12 AM
S
user T
thank you! is there an example project I could look at?Aug 21, 2020, 10:15 AM
T
Sure. There's one in the repo: https://www.github.com/nuxt-community/sanity-module/tree/main/example
Aug 21, 2020, 10:17 AM
T
Ignore the build aliases in the Nuxt config - that's just because it's used as a fixture in the test suite
Aug 21, 2020, 10:18 AM
S
user T
thank you! this seem very useful. I'll give it a closer look and most likely get around my issue with it π±β¨Aug 21, 2020, 10:18 AM
S
user T
that was of great help! I integrated my code, as far as possible, following your example:const query = groq`*[_type == "snippet"]{ _id, title, mainImage, imageUrl, createdAt, releaseDate, body }[0...29]` export default { name: 'Snippets', async fetch() { this.snippets = await this.$sanity.fetch(query) }, data: () => ({ snippets: [] }), }
no snippets though and in console I still get:
Error in fetch(): TypeError: Cannot read property 'fetch' of undefined
π»Thank you very much!
Aug 21, 2020, 11:31 AM
T
How very odd. What are your configuration settings?
Aug 21, 2020, 11:38 AM
S
user T
interesting... but I'm not sure which configurations you are referring. the once in the sanity studio?Aug 21, 2020, 11:42 AM
T
I mean the module configuration
Aug 21, 2020, 11:42 AM
T
Aug 21, 2020, 11:42 AM
T
You'll either need to copy your sanity config into your nuxt folder or pass in your project id. If you haven't done that, it should complain in the terminal when you start Nuxt
Aug 21, 2020, 11:43 AM
S
https://github.com/purplegreen/re-walk/tree/master/web I did copy the sanity.jason in the web folder
Aug 21, 2020, 11:45 AM
T
Looking
Aug 21, 2020, 11:46 AM
S
thanks βΊοΈ
Aug 21, 2020, 11:47 AM
T
Thanks - looks like there was a problem with reading in the json. If you specify the projectId in your nuxt config it will work now, or you can wait five minutes for me to push a fix
Aug 21, 2020, 11:52 AM
S
I can try to do it! thank you!
Aug 21, 2020, 11:52 AM
S
this one right?
{ sanity: { projectId: 'myProject', token: process.env.SANITY_TOKEN } }
Aug 21, 2020, 11:54 AM
T
Perfect
Aug 21, 2020, 11:54 AM
S
Fantastic! it works π Thank you so much π
Aug 21, 2020, 11:57 AM
T
You're welcome π
Aug 21, 2020, 11:58 AM
S
β¨
Aug 21, 2020, 11:58 AM
T
Version with fix now published as 0.3.2
Aug 21, 2020, 12:00 PM
S
cool β‘
Aug 21, 2020, 12:02 PM
S
wonderful I'll refer to it eventually. thank you very much for your help
Aug 21, 2020, 12:16 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.