AntH00
AntH00

Reputation: 13

Unable to fetch tweets using groq/sanity

I'm doing a code-along for a twitter clone and am not able to get tweets. I'm using Sanity and can confirm the data is there, and is fetched w/o error on sanity client. I think the issue is with groq, I've uninstalled, reinstalled it, installed it as a standalone, and I'm still getting an empty array when console logging. Any Ideas?

Here is the getTweets below

import { groq } from 'next-sanity'
import type { NextApiRequest, NextApiResponse } from 'next'
import { sanityClient } from '../../sanity'
import { Tweet } from '../../typings'


const feedQuery = groq`
*[_type =='tweet' && !blockTweet]{_id,...} | 
order(_createdAt desc)`


type Data = {
  tweets: Tweet[]
}

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse<Data>
) {
    const tweets: Tweet[] = await sanityClient.fetch(feedQuery);

  console.log(tweets)
  res.status(200).json({ tweets })
}

Upvotes: 1

Views: 640

Answers (1)

88 Create
88 Create

Reputation: 16

Do you have vision installed? You can run your query and see what comes back without relying on your frontend code. Sanity Vision

The work incrementally in vision trying:

*[_type =='tweet']

Then to see if your conditional is correct:

*[_type =='tweet' && !blockTweet]

Then add the data after in brackets

    *[_type =='tweet' && !blockTweet] {
_id,
...
}

Try this:

const feedQuery = groq`
*[_type == 'tweet']{
  _id,
  ...
}| order(_createdAt desc)`

Upvotes: 0

Related Questions