
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);
// 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);
// Return a 500 error response
return res.json({ success: false, message: 'Internal Server Error', error: err.message });
} };

How are you calling this function?

export async function getFunctionData(functionId) { try { // Trigger the cloud function const execution = functions.createExecution(functionId, '', false);
// 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);
});

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.

I'd do something like
const res = await fetch(function_url);
const data = await res.json();

i'll try that and get back to you...
Recommended threads
- deno 2 Cloud random errors
we have big problems with the functions. although we do not change anything in the function, we have the following random behaviour: - no scope permissions erro...
- Appwrite functions can't connect to data...
I'm trying to create a function that queries my database, but all database operations timeout from within the function, even though CLI access works perfectly. ...
- Error
I'm trying to get sellerId using account.get() in my appwrite function and this is the error message I'm getting: "Failed to send notification to seller 6865bf...
