Back

Server Action Not Working?

  • 4
  • Web
danverbs
4 Mar, 2024, 08:43

I'm trying to implement a simple form to trigger a search to an external API in a server action but all I see is an error: TypeError: e._formData.get is not a function

Everything works as expected in a clean project, but once Appwrite is installed it breaks. Anyone seen this/got a fix?

TL;DR
Developers experiencing issues with server actions not working properly due to errors related to `window` not being defined. The issue seems to arise when using server-side rendering (SSR) in a specific scenario involving Appwrite API. Workarounds include adding `if (typeof window === 'undefined') return;` to affected functions or removing `export const runtime = 'edge'`. Appwrite SDK versions 0.0.35 and 0.0.37 have been released to address some of the issues. Discussions and potential solutions for similar problems are available on various platforms like GitHub, Stack Overflow, and the Next.js forum.
loks0n
11 Apr, 2024, 09:17

It should be working, did you find an error?

Faye
11 Apr, 2024, 09:31

Getting "os" import errors

Faye
11 Apr, 2024, 09:31

i'll send image in a bit

loks0n
11 Apr, 2024, 09:32

Which version of the package is that?

Faye
11 Apr, 2024, 09:33

the one steven said, i saw there's also 0.0.33, but that one is broken as it has no exports

Faye
11 Apr, 2024, 10:57

Heyo!

Please check out this gist :) https://gist.github.com/docimin/48b4ba32e987edda1743aa088e5c43ea

After removing export const runtime = 'edge', it works fine.

Faye
11 Apr, 2024, 10:57

Version "luke-node-appwrite-edge": "0.0.32"

loks0n
11 Apr, 2024, 11:02

Thanks! Taking a look now

Faye
11 Apr, 2024, 11:05

I keep getting spammed with "window is not defined" in the console, other than that not really helpful errors

loks0n
12 Apr, 2024, 10:13

I've published a new version 0.0.35 with some fixes. Hopefully we can merge it soon 🤞

loks0n
12 Apr, 2024, 10:14

cc @here

Faye
13 Apr, 2024, 01:18

Hey, the ping didn't actually ping me, funnily enough.

There's another thing, still related to window is not defined.

Having a page on SSR, with a client component imported using for example this kind of code:

TypeScript
'use client'
import { storage } from '@/app/appwrite-client'

export default function FetchGallery({ name, gallery }) {
  const getGalleryImageUrl = (galleryId: string) => {
    console.log(galleryId)
    if (!galleryId) return
    const imageId = storage.getFileView('655ca6663497d9472539', `${galleryId}`)
    return imageId.href
  }

  const url = getGalleryImageUrl(gallery?.galleryId)
  console.log(url)

  // The rest of the component remains unchanged with conditional rendering based on the data's availability.
  return (
    <img
      src={url}
      alt={name || 'Image'}
      className={`imgsinglegallery mx-auto h-[400px] w-auto max-w-full rounded-lg object-contain`}
    />
  )
}

Will spam "window is not defined" every time you refresh the page. Most likely in the client sdk?

Faye
13 Apr, 2024, 01:19

Again, removing export const runtime = 'edge' will fix this issue, but then CF won't work anymore

Faye
13 Apr, 2024, 14:38

Also, when trying to get documents, I get this error: Error: The 'credentials' field on 'RequestInitializerDict' is not implemented. in CF Pages Using this, importing from your sdk, version 0.0.36:

Faye
13 Apr, 2024, 21:09

I also get this issue when trying to login via OAuth, using the SSR method

Faye
14 Apr, 2024, 00:13

Also, just to clarify, even using the official way of using SSR like this, will not work

loks0n
15 Apr, 2024, 09:35

Not sure why window would be undefined if it is a client component. This error would only appear running on the server.

loks0n
15 Apr, 2024, 09:36

Published a fix for this with 0.0.37

Faye
15 Apr, 2024, 09:43

Will try it soon, thanks!

Faye
15 Apr, 2024, 09:43

The website still works FYI, but it just spams the console and on CF each request will have a message with that in it if you look under functions and follow the real-time logs

loks0n
15 Apr, 2024, 09:54

Can you fix it with if (typeof window === 'undefined') return; in your getGalleryImageUrl? Seems like cloudflare is trying to prerender the component

Faye
15 Apr, 2024, 09:58

It happens locally too, just by adding the edge runtime

loks0n
15 Apr, 2024, 09:59

Oh I see. Could you share your @/app/appwrite-client source?

Faye
15 Apr, 2024, 10:00

it's super simple:

TypeScript
import {
  Client,
  Account,
  Teams,
  Databases,
  Storage,
  Functions,
  Messaging,
  Locale,
  Avatars,
} from 'appwrite'

export const client = new Client()
  .setEndpoint(`${process.env.NEXT_PUBLIC_API_URL}/v1`)
  .setProject(`${process.env.NEXT_PUBLIC_APPWRITE_PROJECT_ID}`)

export const account: Account = new Account(client)
export const teams: Teams = new Teams(client)
export const databases: Databases = new Databases(client)
export const storage: Storage = new Storage(client)
export const functions: Functions = new Functions(client)
export const messaging: Messaging = new Messaging(client)
export const locale: Locale = new Locale(client)
export const avatars: Avatars = new Avatars(client)

export { ExecutionMethod, ID, Query } from 'appwrite'
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