how to search for content in Nuxt 3 with the @nuxt/content module using .json files?

46 Views Asked by At

how to use custom function ?

I use both the standard search function and the custom one, but as a result I get an empty array

custom function in composable

// composable/custom-search-content.ts
export default async function customSearchContent(search: Ref<string>) {
  const runtimeConfig = useRuntimeConfig()
  const { integrity, api } = runtimeConfig.public.content

  const { data } = await useFetch(`${api.baseURL}/search${integrity ? '.' + integrity : ''}.json`)

  const { results } = useFuse(search, data)

  return results
}

file nuxt.config

// nuxt.config.ts

export default defineNuxtConfig({
 modules: ['@nuxt/content'],
  content: {
    documentDriven: true,
    experimental: {
      search: true
    },
    watch: false
  }
})

file .json

// content/search.json

{
  "title": "Hello Content v2!",
  "textFirst": "first",
  "hello": "hello"
}

file component


// component.vue
 
<script setup lang="ts">

const result = await searchContent('hello');
const result2 = await customSearchContent('hello');

</script>

<template> 
<main>
        <pre>{{ result }} </pre>
        <pre>{{ result2 }} </pre>
</main>
</template>

result

[]
[]

result empty array

0

There are 0 best solutions below