Back

Function not getting a correct body

  • 0
  • Self Hosted
  • Functions
Faye
12 Mar, 2024, 09:36

Heyo,

My function for some reason is suddenly failing. Trying it out with postman, the req.body should be filled, but it's not?

TL;DR
Issue: Function is failing because req.body is not being properly populated when testing with postman. Solution: Ensure that the function environment variables are correctly set up and that the request from postman includes the expected body content.
Faye
12 Mar, 2024, 09:37

My function:

TypeScript
import { Client, Databases, Account } from 'node-appwrite';

export const client = new Client();
export const account = new Account(client);
export const databases = new Databases(client);

export default async ({ req, res, log, error }) => {
  client
    .setEndpoint(`${process.env.APPWRITE_API_URL}/v1`)
    .setProject(`${process.env.APPWRITE_PROJECT_ID}`)
    .setKey(process.env.APPWRITE_API_KEY);

  const document = req.body;

  if (!document) {
    error('Invalid request');
    return res.json({ ok: false });
  }

  try {
    await databases.getDocument('db_web', 'users', document?.userId);
    log('User document get');
  } catch (e) {
    if (e.code === 404) {
      try {
        log('Creating user document');
        const response = await databases.createDocument(
          'db_web',
          'users',
          document.userId,
          {},
          [
            `read(\"user:${document.userId}\")`,
            `update(\"user:${document.userId}\")`,
            `delete(\"user:${document.userId}\")`,
          ]
        );
        return res.send(200);
      } catch (e) {
        error(e);
        return res.send(500);
      }
    }
  }
  return res.send(200);
};

If I post to the function using postman, the body is empty.

Faye
12 Mar, 2024, 09:40

self-hosted, 1.5.3

Faye
12 Mar, 2024, 09:43
Faye
12 Mar, 2024, 09:44

cc @Meldiron

Faye
12 Mar, 2024, 09:44

it also doesn't provide the error "invalid request" sometimes, which is before ok: false?

It's like a 50/50 ^^

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