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.

continuation local storage context lose in bookshelf promises

See original GitHub issue

continuation-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:closed
  • Created 8 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
ricardogracacommented, Jan 14, 2016

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.

0reactions
ricardogracacommented, Dec 4, 2019

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:

  1. You’re not returning the Promise created in getSomethingInDB()
  2. There’s no mention of how the getSomethingInDB() is being used
Read more comments on GitHub >

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

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