Environment

Documentation of how to use Meteor.EnvironmentVariable

Meteor runs most app code within Fibers, which allows keeping track of the context a function is running in. Meteor.EnvironmentVariable works with Meteor.bindEnvironment, promises, and many other Meteor API’s to preserve the context in async code. Some examples of how it is used in Meteor are to store the current user in methods, and record which arguments have been checked when using audit-argument-checks.

const currentRequest = new Meteor.EnvironmentVariable();

function log(message) {
  const requestId = currentRequest.get() || 'None';
  console.log(`[${requestId}]`, message);
}


currentRequest.withValue('12345', () => {
  log('Handling request'); // Logs: [12345] Handling request
});
Anywhere
import { Meteor } from 'meteor/meteor'
(meteor/dynamics_nodejs.js, line 111)

Constructor for EnvironmentVariable

Anywhere
import { Meteor } from 'meteor/meteor'
(meteor/dynamics_nodejs.js, line 20)

Getter for the current value of the variable, or undefined if called from outside a withValue callback.

takes a value and a function, calls the function with the value set for the duration of the call

Arguments

value any

The value to set for the duration of the function call

func Function

The function to call with the new value of the

Stores the current Meteor environment variables, and wraps the function to run with the environment variables restored. On the server, the function is wrapped within Async Local Storage.

This function has two reasons:

  1. Return the function to be executed on the MeteorJS context, having it assigned in Async Local Storage.
  2. Better error handling, the error message will be more clear.

Arguments

func Function

Function that is wrapped

onException Function
_this Object

Optional this object against which the original function will be invoked

Edit on GitHub
// search box