Skip to content
Back

Issue with database event trigger for a Function

  • 0
  • Functions
vyuba
19 Aug, 2025, 21:10

I created a function hosted it on aperture connected it to my domain, in the settings I added da collection to listen to any time a new document is created , it triggers the function but doesn’t log the payload, in the docs it’s said to use req.bodyjson but I keep getting this error : SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) at get bodyJson (/usr/local/server/src/server.js:125:21) at Module.default (file:///usr/local/server/src/function/src/main.js:15:26) at execute (/usr/local/server/src/server.js:220:42) at action (/usr/local/server/src/server.js:237:27) at process.processTicksAndRejections (node:internal/process/task_queues:105:5) at async /usr/local/server/src/server.js:26:5

TL;DR
Developers are encountering an issue with logging payload data when a new document triggers a function. The error "Unexpected end of JSON input" occurs despite using req.bodyjson as directed in the docs. This problem may be resolved by ensuring the JSON input is properly formatted before parsing.
vyuba
19 Aug, 2025, 21:11

import { Client, Users } from 'node-appwrite';

// This Appwrite function will be executed every time your function is triggered export default async ({ req, res, log, error }) => { // You can use the Appwrite SDK to interact with other services // For this example, we're using the Users service const client = new Client() .setEndpoint(process.env.APPWRITE_FUNCTION_API_ENDPOINT) .setProject(process.env.APPWRITE_FUNCTION_PROJECT_ID) .setKey(req.headers['x-appwrite-key'] ?? ''); // const users = new Users(client);

// try { // log(req); // Raw request body, contains request data log(JSON.stringify(req.bodyJson)); // Object from parsed JSON request body, otherwise string // log(JSON.stringify(req.headers)); // String key-value pairs of all request headers, keys are lowercase log(req.url); // Full URL, for example: http://awesome.appwrite.io:8000/v1/hooks?limit=12&amp;offset=50 log(req.host); // Hostname from the host header, such as awesome.appwrite.io log(req.port); // Port from the host header, for example 8000 // pcIndex.upsert([ // { // id: '1', // values: [1, 2, 3], // metadata: { // name: 'bunny-bite', // }, // }, // ]); // } catch (err) { // error('Could not list users: ' + err.message); // }

return res.json({ success: true, }); };

vyuba
19 Aug, 2025, 21:11

This is my code it’s just to log the event

Axistro
19 Aug, 2025, 21:16
TypeScript
import { Client, Users } from 'node-appwrite';

// This Appwrite function will be executed every time your function is triggered
export default async ({ req, res, log, error }) => {
  // You can use the Appwrite SDK to interact with other services
  // For this example, we're using the Users service
  const client = new Client()
    .setEndpoint(process.env.APPWRITE_FUNCTION_API_ENDPOINT)
    .setProject(process.env.APPWRITE_FUNCTION_PROJECT_ID)
    .setKey(req.headers['x-appwrite-key'] ?? '');
  // const users = new Users(client);

  // try {
  // log(req); // Raw request body, contains request data
  log(JSON.stringify(req.bodyJson)); // Object from parsed JSON request body, otherwise string
  // log(JSON.stringify(req.headers)); // String key-value pairs of all request headers, keys are lowercase
  log(req.url); // Full URL, for example: http://awesome.appwrite.io:8000/v1/hooks?limit=12&offset=50
  log(req.host); // Hostname from the host header, such as awesome.appwrite.io
  log(req.port); // Port from the host header, for example 8000
  // pcIndex.upsert([
  //   {
  //     id: '1',
  //     values: [1, 2, 3],
  //     metadata: {
  //       name: 'bunny-bite',
  //     },
  //   },
  // ]);
  // } catch (err) {
  //   error('Could not list users: ' + err.message);
  // }

  return res.json({
    success: true,
  });
};

Formatted the next guy who tries to debug

vyuba
19 Aug, 2025, 21:25

Thank you

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