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
- Functions Failing on CRON Schedule
I set up an hourly CRON Schedule for my function and It only executes successfully every after 3 hours sometimes 10 hours. When I execute it manually it works e...
- MongoDb Database Breaks integer[] & bigi...
I've got a table with the below column created. When I try to insert the value `[-3408048000]` into it, the dart sdk cloud function call is successful (no error...
- Appwrite Functions cold start
Hello. I'm seeing really long cold starts on Appwrite Cloud Functions and wanted to know if this is expected. My function just authenticates the user, fetches ...