Back

How to fetch and create documents while using Server Component in NextJs?

  • 0
  • Auth
  • Web
  • Cloud
Rohan
3 Aug, 2024, 06:48

I was following this tutorial by @Dennis Ivy . One thing I didn't understand was why we are creating a new session using createSessionClient before fetching the documents. Why can't we directly fetch it like this:

TypeScript
 const { documents: orders, total } = await databases.listDocuments(
            process.env.NEXT_PUBLIC_DATABASE_ID,
            process.env.NEXT_PUBLIC_COLLECTION_ORDERS
        );

Second question I have is why we are creating an API route to fetch the data from Appwrite DB when we can directly fetch it from the server component to avoid an extra API call? (That's one of the benefits of using a server component.)

Tutorial Link: https://www.youtube.com/watch?v=ENnG7GusuO4 Source Code: https://github.com/divanov11/Nextjs-Appwrite-Auth-SSR/blob/main/src/app/api/orders/route.js

Any help is appreciated!

TL;DR
- Developer is wondering why they need to create a new session using `createSessionClient` before fetching documents in a NextJs server component. - Developer questions the need for creating an API route to fetch data from Appwrite DB when they can directly fetch it from the server component to avoid an extra API call. - Tutorial Link: https://www.youtube.com/watch?v=ENnG7GusuO4 - Source Code: https://github.com/divanov11/Nextjs-Appwrite-Auth-SSR/blob/main/src/app/api/orders/route.js Solution: The session creation step is necessary for authenticating with
Rohan
3 Aug, 2024, 06:49

How to fetch and create documents while using Server Component in NextJs?

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more