Back

[cloud Function] how to use cloud function

  • 0
  • Self Hosted
  • Flutter
  • Tools
  • Functions
conqueror
4 May, 2023, 13:25

visited doc but not get any idea,

TL;DR
The user is asking for help on how to use a cloud function in Appwrite. There is currently no bulk-create feature, so the code needs to be triggered multiple times to create multiple documents. However, there is a feature suggestion for bulk document creation on the official GitHub page. To create a document using a cloud function, a code snippet is provided. It is similar to using Flutter and needs to be run multiple times for each document. The code initializes the SDK, sets the endpoint and project ID, and then creates a document using the database ID, collection ID, and document ID provided. The user asks for ideas on how to
D5
4 May, 2023, 13:31

Basically you create one to make a specific thing with your data or backend and then you trigger it to be executed from an app, an event (database, Auth), manually or scheduled

conqueror
4 May, 2023, 13:36

any basic idea if you can provide like what is happening in this function or what is use for res and req and how to trigger on event or create document from function ```Future<void> start(final req, final res) async { final client = Client();

//Uncomment the services you need, delete the ones you don't final account = Account(client); final avatars = Avatars(client); final database = Databases(client); final functions = Functions(client); final health = Health(client); final locale = Locale(client); final storage = Storage(client); final teams = Teams(client); final users = Users(client);

if (req.variables['APPWRITE_FUNCTION_ENDPOINT'] == null || req.variables['APPWRITE_FUNCTION_API_KEY'] == null) { print( "Environment variables are not set. Function cannot use Appwrite SDK."); } else { client .setEndpoint(req.variables['APPWRITE_FUNCTION_ENDPOINT']) .setProject(req.variables['APPWRITE_FUNCTION_PROJECT_ID']) .setKey(req.variables['APPWRITE_FUNCTION_API_KEY']) .setSelfSigned(status: true); }

res.json({ 'areDevelopersAwesome': true, }); }```

D5
4 May, 2023, 13:39

What it makes is showing a response who is 'areDevelopersAwesome': true

D5
4 May, 2023, 13:40

What's inside the Future<void> function is what's executed

D5
4 May, 2023, 13:40

Other things are requests made to start appwrite services. You can delete what you will not use

conqueror
4 May, 2023, 13:41

do you have any idea for creating document from function so that i can create 10 document in just single click ?

D5
4 May, 2023, 14:02

Same as you would have done with flutter: run this 10 times for each document you need to create:

TypeScript
import 'package:appwrite/appwrite.dart';

void main() { // Init SDK
  Client client = Client();
  Databases databases = Databases(client);

  client
    .setEndpoint('https://cloud.appwrite.io/v1') // Your API Endpoint
    .setProject('5df5acd0d48c2') // Your project ID
  ;
  Future result = databases.createDocument(
    databaseId: '[DATABASE_ID]',
    collectionId: '[COLLECTION_ID]',
    documentId: '[DOCUMENT_ID]',
    data: {},
  );

  result
    .then((response) {
      print(response);
    }).catchError((error) {
      print(error.response);
  });
}
D5
4 May, 2023, 14:04

If I'm not wrong, currently it's not possible to bulk-create documents, so you will need to trigger the above code the same amount of times as documents you will need. However such feature has been suggested: https://github.com/appwrite/appwrite/issues/3051

You can give it a 👍

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