Error with listen function in custom Sanity composable

4 replies
Last updated: Jun 20, 2023
I try to run client.listen() but I get the error “listen is not a function”
Here is the code :


/*
composable for loading and previewing data from sanity 
*/
export default async function ({ query, params, livePreview = true }) {
	const mainStore = useMainStore()
	const sanityClient = mainStore.previewIsActive
		? {
				client: 'preview',
				server: false,
				initialCache: false,
		  }
		: undefined

	// live preview: listen to changes
	onMounted(() => {
		if (mainStore.previewIsActive && livePreview) {
			const sanity = useSanity('preview')
			console.log('rr')
			
			sanity.client.listen(query, params).subscribe((event) => {
				setTimeout(() => {
					refresh()
				}, 900)
			})
		}
	})

	// get data
	const { data, refresh } = await useLazySanityQuery(
		query,
		params,
		sanityClient
	)

	return data
}
Do you know why he tells me that
listen
is not a function?
Thank you
Jun 9, 2023, 2:20 PM
I don’t have an answer, but am providing the source for context since most of these are custom hooks.
Have you pinned the client to the same version as the lock file of the template?
Jun 9, 2023, 6:26 PM
Thank you. @sanity/client wasn’t installed
Jun 12, 2023, 10:34 AM
Glad you’re up and running! 🎉
Jun 12, 2023, 2:30 PM
user Q
i installed @sanity/client but doesn't work for me. Do you have any recomendation?
Jun 20, 2023, 9:13 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?