Navigation

Function Context

MongoDB Stitch functions can interact with connected services, user information, predefined values, and other functions through the context variable.

  • Services like Twilio or GitHub can be accessed through context.services.
  • Functions can execute other functions using context.functions.
  • Information about the requesting user can be accessed through context.user.
  • Information about the HTTP request that triggered a function call can be accessed through context.request.
  • Global variables can be defined in Stitch and referenced with context.values.

context.services

Integrated Services are accessible in functions using context.services.

context.services.get(serviceName)

Returns a service handle object or undefined if no such service exists.

Parameter Type Description
serviceName string The name of the service. This is the name you provided for the service when it was created. For the MongoDB Atlas service, the name is always mongodb-atlas, and for the Push Notification service, the name is always gcm.
Returns:An object with methods to perform service actions. See Functions for a reference of available actions on each service. If the service does not exist, undefined is returned.

Example

When creating a MongoDB Stitch app through Atlas, a default MongoDB service is added and named “mongodb-atlas”.

exports = function() {
   var mongodb = context.services.get("mongodb-atlas");
   return mongodb.db("db").collection("coll").findOne();
};

See also

MongoDB.

context.functions

Other functions defined in MongoDB Stitch may be called using context.functions.

context.functions.execute(functionName, arg...)

Calls another function defined in MongoDB Stitch.

Parameter Type Description
functionName string The name of the function.
arg mixed A variadic list of arguments passed to the function.
Returns:The returned value of the called function.

Example

Suppose the function sum is defined as follows:

exports = function (a, b) { return a + b; };

This can be called from another function difference as follows:

exports = function(a, b) {
    return context.functions.execute("sum", a, -1 * b);
};

context.values

Provides access to constant values defined in MongoDB Stitch.

context.values.get(valueName)

Calls another function defined in MongoDB Stitch.

Parameter Type Description
valueName string The name of the defined value.
Returns:The value associated with the name or undefined if there is no value defined with name valueName.

Example

Define a value named incrementValue and set the value to 5.

Create a new function named increment with the following code:

exports = function (arg) { return arg + context.values.get("incrementValue"); };

context.user

Provides a view of the currently authenticated user. context.user is a document summarized as follows:

{
    "id": <string>,
    "type": <string>,
    "data": <document>,
    "identities": <array>
}

Example

The following is an example context.user document:

{
    "id": "5a09f135b6fc810f19421c12",
    "type": "server",
    "data": {
        "name": "api-key"
    },
    "identities": [
        {
            "id": "5a09f135b6fc810f19421c13",
            "provider_type": "api-key"
        }
    ]
}

context.request

Provides information about the external HTTP request that triggered the function call, which can help you determine the origin of the call. It may contain the following fields:

{
   "remoteIpAddress": <string>,
   "httpReferrer": <string>,
   "httpUserAgent": <string>
}

If there is no referrer header or user agent header present in the request, the respective field will be omitted.

Example

The following is an example context.request document:

{
   "remoteIpAddress": "54.173.82.137",
   "httpReferrer": "https://myapp.example.com/",
   "httpUserAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36"
}