RequestContext always returns undefined
See original GitHub issueDescribe the bug The RequestContext always returns undefined after creating.
Stack trace
To Reproduce Steps to reproduce the behavior:
- Use beneath code as middleware:
const DatbaseContextLoader = async (_req: Request, _res, next) => {
RequestContext.create((await Database.getConnection()).em, next);
console.log("Req context",RequestContext.currentRequestContext());
};
- It always logs
Req context undefined
Expected behavior Return a forked em.
Additional context Using ts-node.
The active property is always undefined, even after just creating.
Versions
Dependency | Version |
---|---|
node | v13.7.0 |
typescript | v3.7.3 |
mikro-orm | v3.0.1 |
your-driver | myslql2 |
Issue Analytics
- State:
- Created 4 years ago
- Comments:10 (10 by maintainers)
Top Results From Across the Web
p:poll returns undefined on "oncomplete" function - jsf
Because the Poll element returns undefined on "oncomplete" function called. I don't understand why args are always undefined. I tried all.
Read more >Identity Map and Request Context | MikroORM
RequestContext. getEntityManager() will return undefined if the context was not started yet.
Read more >args in handleComplete is always undefined
In PF 3.3.RC1 if you define oncomplete="handleComplete(xhr, status, args) and check "args" parameter, it's always "undefined" although ...
Read more >Style the Sign-In Widget - Okta Developer
Note: The getRequestContext() method only returns a value when the Okta-hosted sign-in page is loaded in the context of an application (such as...
Read more >Request | RapidAPI for Mac Documentation - Paw Cloud
function evaluate(context) { var request = context. ... grant type var grantType = oauth2["grant_type"]; // always "authorization_code" return clientId; }.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I completely misunderstood how this works then…
However, no matter where I log it, it’s always undefined: in every handler that’s called with next as well (I placed a log in your code where it retrieved the RequestContext and there it always returns undefined as domain.active is never defined?).
Also, isn’t domain deprecated in Node? https://nodejs.org/api/domain.html
I found out (after lots of headaches) that zone.js (which is used by Angular SSR) was bugging the RequestContext (also in the async_hooks update)… If more people encounter this issue with zone.js, this how to create a forked em within a zone:
In any middleware that is executed befor database calls:
and then you can access the em within the zone with:
Zone.current.get("em")
This requires zone.js to be imported in the top most file (for me it’s
server.ts
):And to stop typescript complaining that types are missing (at least in
zone.js 0.11.1
, see https://github.com/angular/angular/pull/38585) add this to yourtsconfig.json
: