Docs Menu
Docs Home
/ /
Atlas Device SDKs
/
/

Call a Function - C++ SDK

On this page

  • Call a Function By Name

The examples on this page demonstrate calling an Atlas Function named concatenate that takes two arguments, concatenates them, and returns the result:

// concatenate: concatenate two strings
exports = function(a, b) {
return a + b;
};

Important

Make sure to sanitize client data to protect against code injection when using Functions.

To execute a function from the C++ SDK, use the call_function() member function on the user object. Pass in the name of the function as a string for the first parameter. This function takes two arguments, which we provide as a string array of arguments:

// Connect to an App Services App and authenticate a user
auto appConfig = realm::App::configuration();
appConfig.app_id = APP_ID;
auto app = realm::App(appConfig);
auto user = app.login(realm::App::credentials::anonymous()).get();
auto sync_config = user.flexible_sync_configuration();
// If the function takes arguments, pass them as a string array.
// Any quotes within the array must be escaped.
auto argArray = "[\"john.smith\", \"@companyemail.com\"]";
// Call an App Services function as the logged-in user
auto result = user.call_function("concatenate", argArray).get();
// Verify that the result has a value
CHECK(result);
auto functionResult = result.value();
// Prints "Calling the concatenate function returned
// "john.smith@companyemail.com"."
std::cout << "Calling the concatenate function returned " << functionResult
<< ".\n";

The callback can provide an optional string result, or an optional error. In the example above, we check that the result has a value.

← 
 →