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
- After assigning a domain to my Dart func...
I’ve attached the images. Could anyone please explain how this execution is being performed?
- I can't UNPAUSE my project with the free...
I received an email notifying me that my project had been paused due to inactivity, and the email included a link to "Restore project." However, that button red...
- Error trying to deploy functions from CL...
✗ Error • on-auth-create (6a00b2ab000c9ce2949b) • Server Error ✗ Error: No functions were pushed. Prject ID: 69ffe0270033a4ac420a