strapi
strapi copied to clipboard
How to use "useStrapi4" from /server/api?
How can you use Strapi from /server/api/name.post.js
?
I get the error [nuxt] [request error] useStrapi4 is not defined
if i do not import strapi, and if i try to import useStrapi4
from @nuxtjs/strapi
I get the following error [worker reload] [worker init] The requested module 'file://C:/Users/...../node_modules/@nuxtjs/strapi/dist/module.mjs' does not provide an export named 'useStrapi4'
.
Got exactly the same problem
How can you use Strapi from
/server/api/name.post.js
?I get the error
[nuxt] [request error] useStrapi4 is not defined
if i do not import strapi, and if i try to importuseStrapi4
from@nuxtjs/strapi
I get the following error[worker reload] [worker init] The requested module 'file://C:/Users/...../node_modules/@nuxtjs/strapi/dist/module.mjs' does not provide an export named 'useStrapi4'
.
Did you find a solution?
@tiYusupov no, I have not.
Hello,
Did you find a solution ?
If yes : have you an example ?
Thanks.
I did like this: Maybe it's wrong. But working for me. ( Nuxt 3 [3.0.0-rc.7], Strapi 4 )
At frontend
const token = useStrapiToken()
await $fetch('/api/check', {
method: 'post',
body: {
jwt: token.value
}
})
At /server/api/test.post.ts
import { defineEventHandler, readBody } from 'h3'
const runtimeConfig = useRuntimeConfig()
export default defineEventHandler(async (event) => {
const body = await readBody(event)
return await $fetch(`${runtimeConfig.strapi.url}/api/users/me`, {
headers: {
'Content-type': 'application/json',
'Authorization': `Bearer ${body.jwt}`,
},
})
})
Frustrating, I had the same problem
Frustrating, I had the same problem
You can replace nuxt server API logic to strapi API, then request on nuxt page to strapi via client() or something. It's better than use Strapi on Nuxt server API
The same problem. Sometimes it is needed to fetch the data from API that is not publically available, but accessible by read-only token. And also where no user authentication. In this case, exposing the sensitive data (token) on the client side is not an option, so server-side usage is required. So, it would be really good if this module can support data fetching on the server side out of the box as well.
As far as I understood, the Strapi module is accessible from the client-side only. I wanted to use it as well from Nitro, but we do not have access to the composables from server/api
.
I wanted to protect my Strapi API with the API Token and keep the server URL private, but I had to write everything from scratch. I used Strapi's recommended way of using qs
to build my queries alongside $fetch
requests.
Bummer.
Maybe as a Nitro plugin?