Call an Atlas Function
On this page
You can call an Atlas Function from a client application using the Atlas Device SDK for Flutter. Functions are serverless JavaScript functions that let you define and execute server-side logic. These server-side Functions can run in the context of the authenticated user, and thus honor the rules, roles, and permissions that you have assigned to your data in Atlas.
For more information on configuring and writing Atlas Functions, refer to Atlas Functions in the App Services documentation.
Before You Begin
In an App Services App, define an Atlas Function.
In your client project, initialize the App client.
Then, authenticate a user in your client project.
Call a Function
To call a Function, call User.functions.call(). Pass the Function name as the first argument and all arguments for the Function in a List as the second argument.
To include objects as arguments to the Function,
convert them to JSON first. You can do this using the jsonEncode()
function included in the built-in dart:convert
library.
The Function returns a dynamic
value containing MongoDB Extended JSON (EJSON) deserialized to a native Dart object.
final response = await user.functions.call("addition", [1, 2]); // convert EJSON response to Dart number print(response); final responseAsNum = num.tryParse(response["\$numberDouble"]); prints(responseAsNum); // prints 3
Example
The above client code call this Atlas Function running in an App Services App.
// Add two numbers exports = function(num1, num2){ return num1 + num2; };