Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sample call to Directus #4

Open
chiu0602 opened this issue Jan 18, 2024 · 4 comments
Open

Sample call to Directus #4

chiu0602 opened this issue Jan 18, 2024 · 4 comments

Comments

@chiu0602
Copy link
Contributor

This is not a bug report. This repository is excellent but I am not familiar with Nextjs with SSR.

Would you please provide a sample for a page with a button, which will interact with Directus when clicked?

Thanks for your great code.

@DutchCloud4Work
Copy link

+1

2 similar comments
@R1NEJ0
Copy link

R1NEJ0 commented Apr 25, 2024

+1

@cpdavila
Copy link

+1

@huntedman
Copy link

To anyone wondering how to make a client-side request.
Can also be adapted to work when a button is clicked. Also the session is automatically refreshed via directus()

"use client"

import { directus } from "@/services/directus"
import * as React from "react"

import { readItems } from "@directus/sdk"
import { getSession } from "next-auth/react"

export default function Page() {
  const [items, setItems] = React.useState([])

  React.useEffect(() => {
    const fetchItems = async () => {
      const session = await getSession()
      const api = directus(session?.access_token)
      try {
        const response = await api.request(readItems("test"))

        console.log("Resoinseee", response)
      } catch (error) {
        console.error("Error fetching items:", error)
      }
    }

    fetchItems()
  }, [])

  return (
    <div>
      <h1>Directus Query Example</h1>
      <p>Check the console for the logged items.</p>
      <p>Number of items fetched: {items.length}</p>
    </div>
  )
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants