- Stitch >
- Node.js SDK >
- Work with MongoDB Realm
Call a Function¶
Overview¶
MongoDB Realm Functions allow you to run serverless snippets of code. MongoDB Realm Functions are useful for abstracting away implementation details from client applications by doing things such as reusing code-snippets to process data or dynamically performing actions based on the current user, processing data, and more. You can call MongoDB Realm Functions from the client SDK. The active user of a given MongoDB Realm Function execution is the authenticated user that called it.
Create a Function¶
The example in this section demonstrates calling a simple MongoDB Realm
Function named sum
that takes two arguments, adds them, and returns
the result. To learn more about creating a MongoDB Realm Function, read
the Define a Function doc.
Call the Function from a Node.js Application¶
There are two alternate methods for executing a MongoDB Realm Function from the Node.js application.
Method | Usage |
---|---|
<userInstance>.functions.<functionName>() |
Pass the parameters directly to the method, functions.<functionName>() , which you call on the user instance. For example, running the script: “myUserInstance.functions.sum(2,3)” would return the result of 5. |
<userInstance>.callFunction() |
The method callFunction() calls the MongoDB Realm Function on the user object with the first parameter being the Function name and the second parameter being an array of arguments that you pass to the Function. |
Link a MongoDB Atlas Cluster
This example requires a MongoDB Realm Application with a linked Atlas cluster. Replace the “<appId>” in the code block with your app id, available in the lefthand corner of the MongoDB Realm UI.
When you run the code sample, your output should resemble the following: