I have a search page on my Prismic/Nuxt project that makes a full-text query to the Prismic API.
This code will do that
export default {
name: 'Search',
async asyncData({ $prismic, params, query, error }) {
try {
// Query to get post content
const products = await $prismic.api.query($prismic.predicates.fulltext('my.product.title', query.q), { orderings: '[my.product.title desc]' })
// Returns data to be used in template
return {
products: products.results,
}
} catch (e) {
// Returns error page
error({ statusCode: 404, message: 'Page not found' })
}
},
}
The URL is /search/?q=somesearch
The problem is that if I hit /search/ with no query parameter it will immediately hit the catch with the error Unable to encode undefined of type undefined
, obviously because it tries to query the API with an undefined value, but I can't seem to find out how to make a valid check to don't query the API if no query is set and instead just show the search box.
I have tried with
if query.q !== undefined
in the try section but that doesn't work.