Navigation

Context Modules

Overview

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

The context variable contains the following modules:

Property Description
context.services Access service clients for the services you’ve configured.
context.values Access values that you’ve defined.
context.user Access information about the user that initiated the request.
context.request Access information about the HTTP request that triggered this function call.
context.functions Execute other functions in your Stitch application.

Context Modules

context.services

Integrated External 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.

See also

Atlas Overview.

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.

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.

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>
}

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.

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"
}