Back

how do i pass data from a appwrite cloud function to the client sdk?

  • 1
  • Functions
amaturcodrgui
8 Oct, 2024, 13:39

When I use res.json it returns undefined even though when i log it in the executions tab it shows the data just fine?

here is a snippet of code:

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

export default async ({ req, res, log, error }) => { // Initialize SDK const client = new Client(); const databases = new Databases(client);

client .setEndpoint(process.env.APPWRITE_ENDPOINT) // Your API Endpoint .setProject(process.env.APPWRITE_PROJECT) // Your project ID .setKey(process.env.APPWRITE_KEY); // Your secret API key

try { // Log the incoming request body if needed log('Incoming request:', req.body);

TypeScript
// Fetch all collections
const collections = await databases.listCollections(process.env.APPWRITE_DATABASE_ID);

// Use map to create an array of collection names
const collectionNames = collections.collections.map(collection => collection.name);

log(collectionNames)

// Return the collections in the response
return res.json({
  success: true,
  data: collectionNames
});

} catch (err) { // Handle any errors that occur error('Error fetching collections:', err.message);

TypeScript
// Return a 500 error response
return res.json({ success: false, message: 'Internal Server Error', error: err.message });

} };

TL;DR
To pass data from an Appwrite cloud function to the client SDK, ensure you are awaiting the function completion before retrieving output. You may be calling the data before it's resolved, hence seeing 'undefined'. Make sure to structure your function calls accordingly and handle errors properly.
Kenny
8 Oct, 2024, 13:48

How are you calling this function?

amaturcodrgui
8 Oct, 2024, 13:52

export async function getFunctionData(functionId) { try { // Trigger the cloud function const execution = functions.createExecution(functionId, '', false);

TypeScript
        // Wait for the function to complete before retrieving output
        const result = await functions.getExecution(functionId, execution.$id);

        // Log the execution output
        console.log('Function Output:', result);
        return result; // Return the output of the function
    } catch (error) {
        console.error('Error executing function:', error);
    }
}.       onMount(async () => {
    const functionOutput = await getFunctionData(import.meta.env.VITE_DATA_FUNCTION_ID);
    console.log('Additional Function Data:', functionOutput);
});
Kenny
8 Oct, 2024, 13:53

I believe you'll need to call getExecution to check the status until it's resolved. So you're probably calling it before the information has been resolved, that's why you're getting undefined.

Kenny
8 Oct, 2024, 13:54

I'd do something like

TypeScript
const res = await fetch(function_url);
const data = await res.json();
amaturcodrgui
8 Oct, 2024, 13:55

i'll try that and get back to 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