Store arbitrary data on the Context #2159
Unanswered
petermenkeepsilon
asked this question in
Q&A
Replies: 1 comment 4 replies
-
Hello @petermenkeepsilon Just move your data into a sub map. $ctx.set(MY_KEYS, new Map())
$ctx.get<Map<string,any>(MY_KEYS).forEach()
You can also use a service context (Scope request is not necessary since @Injectable()
class MyContext {
@InjectContext()
$ctx: PlatformContext;
$onRequest($initialCtx: PlatformContext) {
$initialCtx.set(MyContext, new Map());
// note $ctx.id can be used as correlationId (uuid)
}
get map(): Map<string, any>{
return this.$ctx.get(MyContext);
}
// add any business method to control stored data in your request context.
setProduct(p: Product){
this.map.set(p);
}
}
see you ;) |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
With Ts.ED v6, my team's application used a custom logger Layout that logged a message and added any values that were added to the Context with
ctx.set
. This worked becauseContext
extendedMap
, so we could set values in a controller or service, and retrieve them withctx.forEach
in the Layout.With v7,
Context
no longer extendsMap
and instead it stores values in a private#cache
variable.ctx.set
still works, but there is no equivalent ofctx.forEach
to retrieve all of the values in the#cache
.Is there a better way to store arbitrary data for the lifetime of a request so it can be used in the Layout?
We are currently looking into moving the data to the response locals or providing a service scoped to the request with
@Scope
to hold this data.Beta Was this translation helpful? Give feedback.
All reactions