Page Contents
Home > @loopback/context > Context > get
Context.get() method
Get the value bound to the given key, throw an error when no value is bound for the given key.
Signature:
get<ValueType>(keyWithPath: BindingAddress<ValueType>, session?: ResolutionSession): Promise<ValueType>;
Parameters
Parameter | Type | Description |
---|---|---|
keyWithPath | BindingAddress<ValueType> | The binding key, optionally suffixed with a path to the (deeply) nested property to retrieve. |
session | ResolutionSession | (Optional) Optional session for resolution (accepted for backward compatibility) |
Returns:
Promise<ValueType>
A promise of the bound value.
Example
// get the value bound to "application.instance"
const app = await ctx.get<Application>('application.instance');
// get "rest" property from the value bound to "config"
const config = await ctx.get<RestComponentConfig>('config#rest');
// get "a" property of "numbers" property from the value bound to "data"
ctx.bind('data').to({numbers: {a: 1, b: 2}, port: 3000});
const a = await ctx.get<number>('data#numbers.a');