CloadIO Function

What is CloadIO function?

Sometimes, we can't get data from CloadIO using the usual method from the client side and we have to use another method. In these cases, we use CloadIO functions. It means we can write some code on CloadIO side and run it in the application.

In general, we use CloadIO functions in some situations.

  • When there is security information in our data, in order to increase security, the data must be processed and received from CloadIO's side.
  • Also, when we want to query more than one field in the received data, we can do this on CloadIO's side.

Notice

For more information about CloadIO functions, visit the CloadIO documentation.

How to add CloadIO functions?

To add a CloadIO function in FrameworkJS projects, we do the following steps in order and carefully:

  1. First, prepare the javascript codes and then make sure that codes do not have syntax errors, because in this case, CloadIO will have problems. It's so important.
  2. Open CloadIO dashboard account by the project's name and password. For more information, visit CloadIO Account.
    Then open the Cloud side ---> Functions in the left menu.
  3. At the top of this page, there is a section named Adding a function. It has some fields as follows:
    • name: It is the function name, and the type is String.
    • key: This is the key that we want to call function on the client side. The type of key is String. It is generally written as a function name in camel case or its abbreviation.
    • description: It contains an explanation of the functionality of the function. It is optional.

    Notice

    Note that the key is unique for every functions.

  4. After filling in the field, click the Add Function button to create an empty code box. This empty box has a name that is added in the previous step.
  5. We should copy the prepared codes without syntax errors here and to save the changes should press the Save all changes button.
  6. Open the Settings section in the left menu and then press the Restart button at the bottom of this page.
  7. Notice

    Note that if you forget the step 6, your changes will not be applied.

  • Example:
    Performing a CloadIO Function
    database.connect(function (error, mongodb) {
        if (error)
            send('connect to database failed');
        else {
            var query = {
                status: 'confirm',
                remove: {
                    $exists: false
                }
            },
            options = {projection: {_id: 0}, sort: {createdAt: -1}};
            mongodb.collection('requests').find(query, options).toArray(function (error, results) {
                if (error)
                    send('getting results was failed');
                else
                    send(null, results);
            });
        }
    });

How to use CloadIO functions?

The CloadIO function is very simple to use on the client side. The function is called with ref.run method. It has three input parameters:

  • key: The first parameter is the key that we wrote for the function in CloadIo as a string, If you need more information read the How to add CloadIO functions section again.
  • arguments: These are the arguments we pass to function and the type of this is Object. The parameters can pass everything that you need, or they can be empty.
  • Notice

    You have to add the empty curly brace at the second entry parameter {} if there are no arguments.

  • callback: The last parameter is a callback function to return the result. This callback also has two entry arguments. The first one is for error handling, the other argument is as an array output.
Parameters Type Description
Name String The text of key that we wrote for the function in CloadIO.
Arguments Object Arguments that we send to CloadIO side function to be used there.
Callback Function It is for error handling and getting the result.
  • Example:
    Performing the use of CloadIO function in the project
    ref.run('request', {createdAt: app.now()}, function (error, results) {
        if (error)
            console.error(error);
        else
            $('.page.request > .result').html('The first result is: ' + results[0]);
    });