question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RequestContext always returns undefined

See original GitHub issue

Describe the bug The RequestContext always returns undefined after creating.

Stack trace

To Reproduce Steps to reproduce the behavior:

  1. 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());
    };
  1. It always logs Req context undefined

Expected behavior Return a forked em.

Additional context Using ts-node.

https://github.com/mikro-orm/mikro-orm/blob/96d771a7799a901fa0594176d64247383ee38418/lib/utils/RequestContext.ts#L31

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:closed
  • Created 4 years ago
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
jsprwcommented, Jan 27, 2020

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

0reactions
jsprwcommented, Sep 24, 2020

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:

const em = orm.em.fork();
Zone.current
    .fork({
      name: "mikro-orm",
      properties: {
        id: em.id,
        em,
      },
      onHandleError: (
        delegate: ZoneDelegate,
        current: Zone,
        target: Zone,
        error: any
      ): boolean => {
        console.error("Database error:", error);
        return false;
      },
    })
    .run(() => {
      next();
    });

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):

import "zone.js/dist/zone-node";

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 your tsconfig.json:

"types": ["node", "zone.js/zone.js"],
Read more comments on GitHub >

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

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found