Notice
For more information about CloadIO functions, visit the CloadIO documentation.
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.
For more information about CloadIO functions, visit the CloadIO documentation.
To add a CloadIO function in FrameworkJS projects, we do the following steps in order and carefully:
Cloud side ---> Functions
in the left menu.
Adding a function
. It has some fields as follows:String
.
String
. It is generally written as a function name in camel case or its abbreviation.
Note that the key is unique for every functions.
Add Function
button to create an empty code box. This empty box has a name that is added in the previous step.
Save all changes
button.
Settings
section in the left menu and then press the Restart
button at the bottom of this page.
Note that if you forget the step 6, your changes will not be applied.
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);
});
}
});
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:
Object
. The parameters can pass everything that you need, or they can be empty.
You have to add the empty curly brace at the second entry parameter {}
if there are no arguments.
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. |
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]);
});