Join us at MongoDB.local London on 7 May to unlock new possibilities for your data. Use WEB50 to save 50%.
Register now >
Docs Menu
Docs Home
/ /
Servicios de aplicaciones

Call a Function - C++ SDK

Los ejemplos de esta página demuestran cómo llamar a un Función Atlas nombrada concatenate que toma dos argumentos, los concatena y devuelve el resultado:

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

Importante

Asegúrese de desinfectar los datos del cliente para protegerlos contra la inyección de código al usar funciones.

Para ejecutar una función del SDK de C++, utiliza la función member call_function() en el objeto user. Introduce el nombre de la función como una string para el primer parámetro. Esta función recibe dos argumentos, que proporcionamos como un arreglo de argumentos en forma de string:

// 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.

Volver

Connect to an App Services App

En esta página