continuation local storage context lose in bookshelf promises
See original GitHub issuecontinuation-local-storage in nodejs is similar to thread local storage in java but is based on chains of Node-style callbacks instead of threads.
continuation-local-storage provides a namespace to set variables. When I try to access these variables inside ‘then’ function of any bookshelf promise,the Namespace loses the active context and I could not find these variables.
My first middleware for each request
var cls = require('continuation-local-storage');
app.use(function (req, res, next) {
var namespace = cls.getNamespace('my session');
bookshelf.transaction(function (tx) {
namespace.run(function () {
namespace.set('transaction', tx);
next();
});
});
});
My next middleware
var namespace = require('continuation-local-storage').getNamespace('my session');
namespace.get('transaction') // transaction defined
new EntityModel({id:1})
.fetch()
.then(function(){
namespace.get('transaction') //transaction undefined
})
Can you give me any idea about the promise library used by bookshelf so that I can try to override it’s ‘then’ function to bind namespace with ‘then’?
Issue Analytics
- State:
- Created 8 years ago
- Reactions:1
- Comments:5 (3 by maintainers)
Top Results From Across the Web
How to work around promises when using continuation-local ...
Yet the very basic usage does not work for me, since the context is completely lost! var createNamespace = require('continuation-local-storage') ...
Read more >continuation-local-storage - npm
Start using continuation-local-storage in your project by running `npm i continuation-local-storage`. There are 435 other projects in the ...
Read more >How CLS should work with promises · Issue #64 - GitHub
If the code inside a .then() callback loses CLS context (due to using a queue or similar), then the shim would NOT correct...
Read more >(Node js) Prevent lost session context node-continuation-local ...
(Node js) Prevent lost session context node-continuation-local-storage in Promise ; session.run(() => {. session.set(UtilHelper.USER_ID, user._id);.
Read more >Continuation Local Storage for easy context passing in Node.js
Passing a context around is tedious. Learn how to transparently pass a context in your express application using continuation local storage.
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
No idea what you’re talking about. Can you be more clear about what that
continuation-local-storage
is?Also, this seems like it’s not related to Bookshelf at all. You are calling that
namespace.get()
thing with a string in the first case and with a variable in the second, which I assume is indeed undefined, since you probably meant to call it also with a string.I’m pretty sure this is in no way related to Bookshelf, but is probably due to the fact that on the second call to
requestContext.get('requestId')
you’re indeed in a different context because:getSomethingInDB()
getSomethingInDB()
is being used