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
- How to Avoid Double Requests in function...
I'm currently using Appwrite's `functions.createExecution` in my project. I want to avoid double requests when multiple actions (like searching or pagination) a...
- Project in AppWrite Cloud doesn't allow ...
I have a collection where the data can't be opened. When I check the functions, there are three instances of a function still running that can't be deleted. The...
- Get team fail in appwrite function
I try to get team of a user inside appwrite function, but i get this error: `AppwriteException: User (role: guests) missing scope (teams.read)` If i try on cl...