Search code examples
content-management-systemsanitygroq

Can't make search query work in Sanity CMS


I am trying to perform a very basic search query with Sanity CMS. This is how the person schema I've created looks like:

export default {
  title: "Person",
  name: "person",
  type: "document",
  fields: [
    {
      title: "Name",
      name: "name",
      type: "string",
    }
  ]
}

I have entered two different Person data. And this is how I try to fetch the data:

const client = sanityClient({
  projectId: 'siaj5ql4',
  dataset: 'production',
  useCdn: true
})

const query = '*[_type == "person"]'

client.fetch(query).then(person => {
  console.log(person)
})

But I get an empty array like so in the console: [] There is no error or anything. Any ideas on this simple task?


Solution

  • There are two common reasons for this:

    1. The dataset is private and the client is not configured with a token.
    2. The documents you expect to see is not published (drafts are private by default) and the client is not configured with a token.

    Also note that the CDN can not be used with private datasets and/or access token.