Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Context undefined when used with routing-controllers #32

Open
davidpablosg opened this issue Jan 17, 2019 · 2 comments
Open

Context undefined when used with routing-controllers #32

davidpablosg opened this issue Jan 17, 2019 · 2 comments

Comments

@davidpablosg
Copy link

davidpablosg commented Jan 17, 2019

I've configured the context on ExpressJs app as a dependency on startup and also set it up as global middleware for every request.
The code works like a charm when using routing-controllers for Get, Put and Post
But, using @Body decorator into a PUT/POST method, it becomes undefined.
Also using body-parser, injecting decorator @JSON, the context get lost.

Working cases:

@get('/data')
public getData():Promise {
httpContext.get('contextId');//Got data
return new Promise;
}

@get('/data')
public getData(@Req() req):Promise {
httpContext.get('contextId');//Got data
var a = req.a;
return new Promise((resolve) => getData(a){
resolve(response);
});
}

@post('/data')
public setData():Promise {
httpContext.get('contextId'); //Got data
var a = 'some random data';
return new Promise((resolve) => setData(a){
resolve(response);
});
}

@post('/data')
public setData(@Req() req):Promise {
httpContext.get('contextId'); //Got undefined
var a = req.a;
return new Promise((resolve) => setData(a){
resolve(response);
});
}

Not working cases:

@UseBefore(json())
@post('/data')
public setData(@Req() req):Promise {
httpContext.get('contextId'); //Got undefined
var a = req.a;
return new Promise((resolve) => setData(a){
resolve(response);
});
}

@post('/data')
public setData(@Body() data):Promise {
httpContext.get('contextId'); //Got undefined
return new Promise((resolve) => setData(data){
resolve(response);
});
}

@put('/data')
public setData(@Body() data):Promise {
httpContext.get('contextId'); //Got undefined
return new Promise((resolve) => setData(data){
resolve(response);
});
}

@jungssunkim
Copy link

jungssunkim commented Mar 11, 2019

@davidpablosg
It may be a different problem.
httpContext middleware must use after body-parser.

reference: #4

@zhujun24
Copy link

I have write a tidy npm package http-request-context to do this, using async_hooks, u can try it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants