SR
Click to open github profile
Usage

Manual Queries

Learn how to manually query Craft CMS data in Nuxt


In this guide, we’ll cover how to use manual queries with the useCraftQuery() composable in Nuxt. This approach gives you direct control over the data you fetch, allowing you to display specific fields and customize your queries.

Write a query

Using useCraftQuery(), we can use the Craft CMS query builder for specific data. Here’s how to set up a query to fetch a list of related news articles:

This query underneath will return three entries from the news section, containing only the title field. The await keyword is used to wait for the query to complete, and any errors are logged.

const { data, error } = await useCraftQuery('entries')
  .section('news')
  .fields(['title'])
  .limit(3)
  .all()

if (error.value) {
  console.error(error.value)
}

console.log(data.value)
Tip

Find out more about the available query methodes in the useCraftQuery() docs.


Copyright © 2024 Samuel Reichör