Docs Menu

Docs HomeDevelop ApplicationsAtlas Device SDK

Call a Function - React Native SDK

On this page

  • Before You Begin
  • Call a Function

The examples in this section demonstrate calling a simple Atlas Function named sum that takes two arguments, adds them, and returns the result:

// sum: adds two numbers
exports = function(a, b) {
return a + b;
};
  1. In an App Services App, define an Atlas Function.

  2. In your client project, initialize the App client.

  3. Then, authenticate a user in your React Native project.

Important

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

To call a function, you can either pass its name and arguments to User.callFunction() or call the function as if it were a method on the User.functions property.

import React from 'react';
import {useUser} from '@realm/react';
function Addition() {
// Get currently logged in user
const user = useUser();
const addNumbers = async (numA: number, numB: number) => {
// Call Atlas Function
// Method 1: call with User.callFunction()
const sumMethod1 = await user?.callFunction('sum', numA, numB);
// Method 2: Call with User.function.<Function name>()
const sumMethod2 = await user?.functions.sum(numA, numB);
// Both methods return the same result
console.log(sumMethod1 === sumMethod2); // true
};
// ...
}
←  Connect to an Atlas App Services App - React Native SDKQuery MongoDB - React Native SDK →